Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By std10093
  • 1 Post By Barney McGrew

FILE pointer..problem with fscanf

This is a discussion on FILE pointer..problem with fscanf within the C Programming forums, part of the General Programming Boards category; Hello, I am new to file pointers. All I have to do is copy the values from one file (a1.txt), ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    FILE pointer..problem with fscanf

    Hello, I am new to file pointers. All I have to do is copy the values from one file (a1.txt), add 50 to each of the numbers and save it to another file (a2.txt)
    a1.txt looks like:
    11
    12
    13
    ....
    25

    a2.txt should look like:
    61
    62
    63
    ...
    75


    The problem is fscanf is picking up only 1 instead of 11 in the first line of a1.txt and so 51 is getting printed instead of 61 in a2.txt. However, it is working fine for the subsequent lines. I am unable to figure out the problem...Kindly help...

    here is the code:
    Code:
    #include<stdio.h>
    void main(){
    
    FILE *fp1,*fp;
    
    int i;
    char ch;
    
    fp=fopen("a1.txt","r");
    fp1=fopen("a2.txt","w");
    
    
    while (1)
    {
        ch = fgetc( fp ) ;
        if(ch==EOF)
                   break;
    
      
        fscanf(fp,"%d",&i);
    
        fprintf(fp1,"%d\n",i+50);
     }
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > ch = fgetc( fp ) ;
    While your intent may be to burn a \n at the end of every line, and return EOF when you reach the end of file, it also consumes the very first character of the file.

    Consider something like
    while ( fscanf(fp,"%d",&i) == 1 ) fprintf(fp1,"%d\n",i+50);

    Also, main returns and int, not void.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Thanks so much..I was not aware of this point...

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    • main function should return an int ,not void
    • right before the main function terminates,you should write return 0; in order to state that the program exited fine
    • what does fgetc do?Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.
      (source : fgetc - C++ Reference )
    • so you retrieve one char,thus the first digit from eleven and then you check if it is EOF.if it is not you just forget about it,and so does your program too
    • guess what will happen if you place fgetc and the check after the fscanf and fprintf! now think.....Did you think?Ok you did i hope -> the answer is that it will perform the operation that you desire,but it will write one
      more number in a2.txt,because the check is performed after the fscanf and fprintf

    Hope that helps

    edit - > how slow am i! when started typing no posts were above me :P

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    ..but thanks nevertheless for the explanations...got the point !

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    int readint(int *d, FILE *stream)
    {
        int r;
    
        while ((r = fscanf(stream, "%d", d)) != 1)
            if (r == EOF || getc(stream) == EOF)
                return EOF;
        return 0;
    }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Barney McGrew View Post
    Code:
    int readint(int *d, FILE *stream)
    {
        int r;
    
        while ((r = fscanf(stream, "%d", d)) != 1)
            if (r == EOF || getc(stream) == EOF)
                return EOF;
        return 0;
    }
    It would be nice to explain your code.Remember that the goal is not to make the other just copy paste the code!The goal is that the other to learn....And better yet,to give him hints in order to produce his own code

    You know what a chinese emperor said once :
    Give to your people a number of fishes,and they temporarily aren't going to be hungry.
    Learn your people how to fish,and then they don't have to depend on you for giving them fishes.

    PS - maybe he was japanese
    PS - i can not recall it exactly,but it is something like this

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Sanchari Sircar View Post
    ..but thanks nevertheless for the explanations...got the point !
    You are welcome Sanchari! Also welcome to the forum

  9. #9
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    @std10093:

    fscanf, when given the format string "%d", will return one of three values. It returns 0 to indicate that the conversion failed (so you might handle this by advancing the stream's position by one with a call to getc), 1 to indicate that the conversion was successful, and EOF to indicate that there's either no more data in the stream, or that an error occurred which prevents you from reading data from the stream. That function will discard data from the stream until fscanf successfully matches and converts an integer, or getc/fscanf return EOF.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by Barney McGrew View Post
    @std10093:

    fscanf, when given the format string "%d", will return one of three values. It returns 0 to indicate that the conversion failed (so you might handle this by advancing the stream's position by one with a call to getc), 1 to indicate that the conversion was successful, and EOF to indicate that there's either no more data in the stream, or that an error occurred which prevents you from reading data from the stream. That function will discard data from the stream until fscanf successfully matches and converts an integer, or getc/fscanf return EOF.
    Oh now you think i didn't know what the code did :P Let me help you to get the point

    The point is to give the beginners the explanation needed in order they understand how things work and as a result make them able to produce their own code.

    What you do is understandable by me,because when i started writing here i did the same mistake as you.But the point is not to show your skills,but to help

  11. #11
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I believe the best way to learn is to read and write code. The behaviour of the function I wrote can be learned easily by reading the documentation for fscanf; either in man-pages, the standard documents, or a C reference. The whole point of using a programming language is to express computation, and using English for such purposes sort of defeats the purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2010, 02:18 AM
  2. fscanf with pointer of strings *string[]
    By simone.marras in forum C Programming
    Replies: 7
    Last Post: 03-21-2009, 11:36 AM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. file input and file pointer problem
    By drater in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 09:43 AM
  5. file: fscanf problem
    By catatonicalvin in forum C Programming
    Replies: 4
    Last Post: 03-08-2006, 03:36 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21