Thread: fprintf failing miserably, please help

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    15

    fprintf failing miserably, please help

    Hello,

    I have a program that, shortening it to get rid of some file reads, looks as follows:
    Code:
      int ch;
      FILE *fp;
      fp=fopen("c:\\test.txt", "r+");
      FILE *finuse;
      finuse=fopen("c:\\finuse.txt", "w+");
      if(finuse == NULL) printf("%s", strerror(errno));
      system("PAUSE");
      if(fp == NULL) printf("%s", strerror(errno));
      else
      {
        int InStats=0;          //value to indicate position inside stats loop
        int i=0;                //Counter
        while((ch=fgetc(fp)) != EOF)
        {
           /*lots of testing of ch to position me within the file
     and get some other data I'm not worried about at the moment*/
    
                  else if(ch=='\n' && InStats>9)
                {
                  fprintf(finuse,"inuse: %s",inuse);
                  fflush(finuse);
                }
        }
      fclose(fp);
      fclose(finuse);
      return 0;
    I tried moving the finuse open and close to position them immediately around the fprintf statement, and that works great (except for the fact that I only get one line. This thing should print out 95 lines, but can only do so if I can keep finuse open across the entire iteration of fp reads).

    So, am I missing something? Or, is there a way I can buffer all of this into something else (if I use a string, it has to be capable of holding 297,600 characters).

    Thank you. Understand I'm a complete noob, so please be gentle.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the first thing I see is that you're checking fp for NULL long after you attempt to open the file, and in fact, after you try to open another file, making the value of errno completely useless when checking fp. if you're getting to the loop, then both files must be open, but beyond that, it's hard to say what exactly is going on here. another thing I see is that you're never incrementing InStats. with the given code, InStats can never be greater than 9, and I don't see where you're declaring inuse.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    15
    Quote Originally Posted by Elkvis View Post
    the first thing I see is that you're checking fp for NULL long after you attempt to open the file, and in fact, after you try to open another file, making the value of errno completely useless when checking fp. if you're getting to the loop, then both files must be open, but beyond that, it's hard to say what exactly is going on here. another thing I see is that you're never incrementing InStats. with the given code, InStats can never be greater than 9, and I don't see where you're declaring inuse.
    Ah, good catch on the NULL, although I moved those statements around a bit to see if I could help alievate the problem and didn't. I tried to change it as follows and the program won't print anything to the screen other than the system pause:
    Code:
      FILE *finuse;
      finuse=fopen("c:\\finuse.txt", "w+");
      if(finuse == NULL) printf("%s", strerror(errno));
      system("PAUSE");
      FILE *fp;
      fp=fopen("c:\\test.txt", "r+");
      if(fp == NULL) printf("%s", strerror(errno));
      else
      {
    Not sure what on earth is causing that, but just putting it back to the way I posted above lets it print to the screen just fine (albeit still without editing the file).

    As for InStats - yes, that's part of the code I omitted. I can confirm that it's working and incrementing, though, as if I do a printf instead of an fprintf (omitting finuse in the statement), it prints to the screen perfectly.
    Last edited by reillan; 01-29-2014 at 05:24 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by reillan View Post
    I tried moving the finuse open and close to position them immediately around the fprintf statement, and that works great (except for the fact that I only get one line. This thing should print out 95 lines, but can only do so if I can keep finuse open across the entire iteration of fp reads).
    Since the first thing fopen does is delete the file if it already exists, then that seems perfectly normal. If you don't want to delete the file every time you (re)open it, you need to use the "a" mode.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    15
    Woot, that fixed it. Thank you much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateWindowEx() failing with 64-bit
    By domokato in forum Windows Programming
    Replies: 7
    Last Post: 07-02-2009, 07:30 PM
  2. VS C++ 2008 failing o.O
    By Akkernight in forum Tech Board
    Replies: 7
    Last Post: 01-27-2009, 10:06 AM
  3. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  4. LoadBitmap failing
    By zid in forum Windows Programming
    Replies: 7
    Last Post: 10-10-2005, 09:44 AM
  5. GetDIBits() failing
    By Epo in forum Windows Programming
    Replies: 3
    Last Post: 09-19-2005, 12:08 PM