I have a text file that looks like this:
Code:
hello world
hello.worldfull
this.12345678.87654321
fullness!
And my program will basically just open the file, open an output file (name determined by incrementing the 4th to last letter of the filename), and then copy over string by string using fgets and fprintf. Oddly enough, here's the output file:
Code:
hello world
this.12345678.87654321
fullness!
Notice the second line is missing. I looked at the hex code of the file and there's nothing funky about it, every line is terminated with a 0D0A pair, including the line that gets wiped. Thus, I suspect there is something wrong with my code... so here it is:
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *f, *t;
char fn[256];
char *s,*k;
if (argc < 2) {
printf("Please pass in the file as the argument.");
return -1;
}
strcpy(fn,argv[1]);
int p = strlen(fn);
fn[p-5]++;
if ((f = fopen(argv[1],"r")) == NULL) {
printf("Input File cannot be opened.");
return -2;
}
if ((t = fopen(fn,"w")) == NULL) {
printf("Output File cannot be created.");
fclose(f);
return -4;
}
while (fgets(s,256,f) != NULL) {
fprintf(t,"%s",s);
}
fclose(t);
fclose(f);
return 0;
}
Notice, the line near the end that says
fprintf(t,"%s",s);
I've also tried replacing it with
fputs(s,t);
with the exact same effects (line 2 is missing)
And yes, I do need to approach it this way, copying over a line at a time, because I will eventually be tokenizing the string and doing stuff with the tokens, but I have that under control.
Thanks in advance
-IsmAvatar