CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2003
    Location
    china
    Posts
    53

    Unhappy The problem with fprintf

    I write some codes below
    FILE * fp;
    int j=0;
    char FileName[100]={0};

    sprintf(FileName,"C:\\mttemp.txt");

    if((fp=fopen(FileName,"a+"))!=NULL)
    {

    j=fprintf(fp,"hello\n");
    fclose(fp);
    }

    After execute it , var j is 6,and the new file C:\mttemp.txt was created,but there is nothing in the file C:\mttemp.txt.
    What's wrong?

  2. #2
    Join Date
    Jul 1999
    Location
    Munich, Germany
    Posts
    142

    Re: The problem with fprintf

    Hi.

    Your code works fine on my system. When do you view the file? You have to wait until the call to fclose( fp ) is finished. Since you used buffered I/O the text is first written to some buffer and not directly to the file. Check the contents of your file AFTER fclose(fp).

    Peter

  3. #3
    Join Date
    Oct 2003
    Location
    Romania
    Posts
    127

    Re: The problem with fprintf

    ...or call fflush after fprintf.

  4. #4
    Join Date
    Jun 2003
    Location
    china
    Posts
    53

    Re: The problem with fprintf

    Thanks very much, Peter and marsh. i have resolved the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured