I'm trying to write a program that can open all given files in a directory, and output the files with some changes to another directory given a path specified by the user. I made a function that will be called after fork()
for each filename encountered in the current directory, and the output will have the same name in this new directory. I have looked all over, and I still can't find a reason as to why fgets()
keeps returning NULL
.
void sorter(char *fileName, char *directory, char* newName){
FILE *edit = fopen(fileName, "r");
char buf[700];
char *bufp = buf;
char *fLine = fgets(bufp, sizeof(buf), edit);
if (edit == NULL){
exit(EXIT_FAILURE);
}
printf("%s\n", fLine);
chdir(directory);
FILE *output=fopen(newName, "w");
while(fLine){
fprintf(output, fLine);
fprintf(output, "done");
}
fclose(output);
fclose(edit);
}
转载于:https://stackoverflow.com/questions/53038017/why-does-fgets-continuousely-return-null