argv0 = "delreves";
int m;
for(m = 1;m < argc;m++){
char buffer[256];
FILE *fd;
fd = fopen(argv[m],"r");
if(fd==NULL){
Error (EX_NOINPUT, "%s", "Fallo");
}
while(fgets(buffer,256,fd)){
int size = strlen(buffer);
int l=size-1;
int j;
for(j = 0; j < size/2; j++, l--){
char temp = buffer[j];
buffer[j] = buffer[l];
buffer[l] = temp;
}
printf("%s",buffer);
}
fclose(fd);
}
}
After running the program with a days of week file I have
./a.out file.txt
yadnom
yadseut
....
How can I eliminate the empty newline before the first day?
The function fgets
is returning the string ending with the newline character, if encountered in the input file. Your algorithm is reversing the string, preserving the newline, so it is coming as a first character of the output. An this is what you see: newline and then the string. If you want to eliminate it, just discard the last character of the input (replace it with \\0
), or work with length-1.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.