fgets vs gets
Can someone explain why gets will work and not fgets. The output of fgets() shows the filename entered, but it will not open it.
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
int main()
{
FILE* newFile;
char buffer[80];
char fileName[80];
printf("Enter a file to open: ");
fgets(fileName,80,stdin);
printf("%s",fileName);
newFile=fopen(fileName,"r");
if ( ! newFile) {
perror("NewFile error: ");
exit(1);
}
while ( fgets(buffer,80,newFile) != NULL) {
printf("%s",buffer);
}
fclose(newFile);
return 0;
}
|