file copy
My problem is that when trying to copy from one file to another
the last line keeps looping, never getting to the expected NULL
value at the end of the file when using fgets. The following
is the code snippet where this is happening followed by the
input file and output file.
input_file_pointer = fopen("infile.txt", "r");
output_file_pointer = fopen("refined.txt", "w");
// read
do
{
fgets(line_from_input, maximum_line_length, input_file_pointer);
fputs(line_from_input, output_file_pointer);
}while(line_from_input != NULL);
// close
fclose(input_file_pointer);
fclose(output_file_pointer);
input text file
-------------------------------------------------
dadadajtfjf
awfaefea
gsrhddrh
gfsghrdhjtfdr
sgrgrgtfjf
rsgrhdhrd
srghrdhd
srgrhgr
shgrdhrdhd
rhrhdhrd
dhhddhrdhd
tdhtrdhr
rdhhdhthd
dgdgfdhdtgt
h
output text file
------------------------------------
dadadajtfjf
awfaefea
gsrhddrh
gfsghrdhjtfdr
sgrgrgtfjf
rsgrhdhrd
srghrdhd
srgrhgr
shgrdhrdhd
rhrhdhrd
dhhddhrdhd
tdhtrdhr
rdhhdhthd
dgdgfdhdtgt
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
line_from_input will never be assigned NULL. What you need to do is check the return value of fgets, this is what will return NULL at the end of the file.
Something like:
You can factor the loop to make things a bit neater, but I'm just illustrating the point here. The pointer line_from_input will never change, only the memory it points to is changed. fgets can only return NULL, it can't change line_from_input to NULL.
Something like:
do{if (fgets(line_from_input, maximum_line_length, input_file_pointer) == NULL) { break;}fputs(line_from_input, output_file_pointer);}while(true);
You can factor the loop to make things a bit neater, but I'm just illustrating the point here. The pointer line_from_input will never change, only the memory it points to is changed. fgets can only return NULL, it can't change line_from_input to NULL.
!! Don't abuse NULL like that. The return from fgets is an integer (the number of characters read), not a pointer. Check against 0 instead. Better yet, don't explicitly say what you're checking against; you just want it to be non-zero, and "if (fgets(...))" is idiomatic - "if possible to get a string from the file...".
Like he said above, search for EOF (or '/0' when working with strings) to get the
end, not NULL
end, not NULL
fgets()/fputs() are very odd functions to use for file copying. I'd always prefer fread() and fwrite() under those circumstances (because there's no reason to make your program scan for line ends).
Quote:Original post by Zahlman
!! Don't abuse NULL like that. The return from fgets is an integer (the number of characters read), not a pointer. Check against 0 instead. Better yet, don't explicitly say what you're checking against; you just want it to be non-zero, and "if (fgets(...))" is idiomatic - "if possible to get a string from the file...".
I call bullshit.
But, yes, there are better ways to do this.
Windows API for the win! (non-contender if this is to be platform independant code) Note that you should include your own error handling, as I have neglected to put any in.
Heh, that'll teach you to not to give all the information. You'll note that while this answers your question, if I am not wrong, it does not solve your real problem. Which is that you actually are processing the input file instead of just copying it. Give people the info they need, and you get good answers, as opposed to correct answers that don't help.
#include <windows.h>BOOL result = ::CopyFile(_T("infile.txt"), _T("refined.txt"), TRUE);
Heh, that'll teach you to not to give all the information. You'll note that while this answers your question, if I am not wrong, it does not solve your real problem. Which is that you actually are processing the input file instead of just copying it. Give people the info they need, and you get good answers, as opposed to correct answers that don't help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement