我对C很陌生,有人“挑战”我尝试使用C创建排序程序。我来自语言在做这种事情的时候更高层次更容易,但我认为更低层次的错综复杂性比我的头脑要好。我还没有实现分类,因为我一路上遇到了障碍(只有其中一个)。C - 不能将文件存储到字符串数组中(基于脱行)
不管怎么说,这里是我的代码至今:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *unsortedFile; /* prepare file variable */
char lineBuffer[100]; /* prepare variable for each line */
char *listOfLines[100]; /* prepare line array variable to be sorted */
int n = 0;
int i;
if (argc == 2) /* if a file has been given */
{
unsortedFile = fopen(argv[1], "r"); /* open it readonly */
if (unsortedFile == NULL) /* if it couldn't open */
{
printf("Couldn't open the file %s\n", argv[1]);
printf("Does it exist?\n");
return -1; /* stop the program here, return non-zero for error */
}
printf("original file:\n\n");
while (fgets(lineBuffer, sizeof(lineBuffer), unsortedFile))
{
printf("%s", lineBuffer);
listOfLines[n] = lineBuffer; /* store line buffer to the array */
n = ++n; /* increase n for the next array element */
}
printf("\nLines to be sorted: %d\n", n);
for (i = 0; i < n; i++)
{
printf("%s", listOfLines[i]);
}
} else /* if no or too many args provided */
{
printf("\nArgument error - you either didn't supply a filename\n");
printf("or didn't surround the filename in quotes if it has spaces\n\n");
return -1; /* return non-zero for error */
}
}
在这一点上,你可能比你见过的最凌乱的意大利面条代码忙碌呕吐......但不管怎么说,这个问题发生在while
声明,我猜。原始文件打印到控制台罚款,但我不认为每行都被存储到listOfLines
。
这里是什么在file.txt
,我提供作为参数传递给该程序的文件:
zebra
red
abacus
banana
,这里是该程序的输出:
[email protected]:/mnt/c/Users/Dustin/projects/c/sort$ ./sort file.txt
original file:
zebra
red
abacus
banana
Lines to be sorted: 4
banana
banana
banana
banana
[email protected]:/mnt/c/Users/Dustin/projects/c/sort$
貌似最后一行的文件是唯一一个存储到listOfLines
?什么会导致这种行为?
在此先感谢!
使用'getline',而不是'fgets'。 – BLUEPIXY
@BLUEPIXY你确定OPs平台有吗?看起来像MinGW,所以也许不是。 – hyde