Connecting Tech Pros Worldwide Forums | Help | Site Map

Avoid duplicates at the end of the file during read

Andr3w's Avatar
Member
 
Join Date: Nov 2007
Location: Greece
Posts: 41
#1: Mar 4 '08
Hi,

I was working on something when I noticed that the following code produced a duplicate char before reaching at the end of the file if it had a blank line (with no chars at the file) before the end of the file and without it, it would run file.

Expand|Select|Wrap|Line Numbers
  1. int ReadFromFile(int argc, char **argv)
  2. {
  3.     // our file pointer, initialized at NULL
  4.     FILE *fStream = NULL;
  5.  
  6.     char buf[10];
  7.     char p;
  8.     int i = 0;
  9.  
  10.     if( (fStream = fopen(argv[1], "r")) == NULL )
  11.     {
  12.         // error, return the function
  13.         return FALSE;
  14.     }
  15.  
  16.         while( !feof( fStream ) )
  17.         {
  18.             fscanf(fStream, "%c", &p);
  19.  
  20.  
  21.             if(p != '\n')
  22.             {
  23.                 buf[i] = p;
  24.                 i++;
  25.             }else {
  26.                 buf[i] = '\0';
  27.                 i = 0;
  28.             }
  29.  
  30.             printf("%c", p);
  31.  
  32.         }
  33.  
  34.  
  35.     return TRUE;
  36. }
  37.  
The input file has the following lines

lol
asdf
mmd
(here was the blank line)

the output was:

lol
asdf
mmdd

if the blank line was present, when it didn't have that line the output was
lol
asdf
mmd

Why does this happen I have an idea but I've tried to make a workaround and came at a dead end... Any help?

Update: found a workaround but still I can't explain to myself why the above code fails to provide the output I like... The workaround is the following code

Expand|Select|Wrap|Line Numbers
  1. int ReadFromFile(int argc, char **argv)
  2. {
  3.     // our file pointer, initialized at NULL
  4.     FILE *fStream = NULL;
  5.  
  6.     char strBuf[20];
  7.     char intBuf[10];
  8.     char p;
  9.     int i = 0, j = 0;
  10.  
  11.     if( (fStream = fopen(argv[1], "r")) == NULL )
  12.     {
  13.         // error, return the function
  14.         return FALSE;
  15.     }
  16.  
  17.         do
  18.         {
  19.             p = fgetc(fStream);
  20.  
  21.             strBuf[i] = p;
  22.             i++;
  23.  
  24.                 if(p == '\n' || p == EOF)
  25.                 {
  26.                     strBuf[i-1] = '\0';
  27.                     i = 0;
  28.                 }
  29.  
  30.         }while( !feof(fStream) );
  31.  
  32.  
  33.  
  34.     return TRUE;
  35. }
  36.  
I am sure the problem lies with fscanf but I can't figure why this is happening...

Expert
 
Join Date: Aug 2007
Posts: 674
#2: Mar 4 '08

re: Avoid duplicates at the end of the file during read


Try answering a few questions for me, and we'll see where to go from there.

1. How does feof work? How do you know it works like that?

2. For fscanf, you require a character. What if someone types in value fit for a double instead? What would fscanf do? How do you know that fscanf did not receive something that could be translated as a character?

3. Buffer takes in 9 characters, right? What if someone types in 21 chars, and then hits enter. What happens in your code?

4. Are you aware of the function fgets? What does it do?

5. Yet another question: what is the return value of fgetc? Why is it so?
Reply