fprintf and fscanf error

This is a discussion on fprintf and fscanf error within the C Programming forums, part of the General Programming Boards category; I'm a beginner and I've been confused with the error for two days. Can somebody help me to point out ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    fprintf and fscanf error

    I'm a beginner and I've been confused with the error for two days.

    Can somebody help me to point out which part of my code goes wrong? Thanks.

    code:
    Code:
    int main()
    {FILE *fr;
    fr=fopen("d:\\doc.txt","r+");
    if (fr==NULL)
        {printf("Error");
    exit(1);}
    char s[256];
    fscanf(fr, "%s", s);
    printf("The text is %s", s);
    fprintf(fr, "asdd");
    fscanf(fr, "%s", s);
    printf("\nThe text now is %s", s);
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Between writing and reading, you need to use
    fflush(fr);

    > printf("\nThe text now is %s", s);
    What do you expect to see?
    If the original file was "Hello", then you could be looking at "Helloasdd", "asdd", "asddo" or maybe even something else.

    Perhaps you should have
    fflush(fr);
    fseek(fr,0,SEEK_SET);


    Then the 2nd fscanf will re-read the original text and your appended text.
    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
    Jan 2013
    Posts
    2
    May I know the why I cannot read immediately after writing? What's the principle behind it? I was learning the tutorial from this website but it did not explained anything. Thank you.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > May I know the why I cannot read immediately after writing?
    Because that's what it says in the standard and the manual pages.
    Quote Originally Posted by c99 on fopen
    When a file is opened with update mode (’+’ as the second or third character in the
    above list of mode argument values), both input and output may be performed on the
    associated stream. However, output shall not be directly followed by input without an
    intervening call to the fflush function or to a file positioning function (fseek,
    fsetpos, or rewind), and input shall not be directly followed by output without an
    intervening call to a file positioning function, unless the input operation encounters end-
    of-file. Opening (or creating) a text file with update mode may instead open (or create) a
    binary stream in some implementations.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf and fprintf - white space problem :|
    By Johnny_010 in forum C Programming
    Replies: 7
    Last Post: 04-05-2012, 05:12 PM
  2. FSCANF/FPRINTF Skip Line and Print
    By Ditrik in forum C Programming
    Replies: 7
    Last Post: 01-08-2012, 11:43 AM
  3. Using 'For' with fprintf/fscanf
    By Pecado in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2010, 06:38 AM
  4. How to write in a file using fscanf and fprintf
    By g_p in forum C Programming
    Replies: 8
    Last Post: 05-24-2007, 01:52 PM
  5. Help about fopen,fscanf,fprintf
    By bluexrogue in forum C Programming
    Replies: 1
    Last Post: 09-03-2001, 08:05 PM

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