Thread: Nothing is written to file

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Nothing is written to file

    Thanks for your help earlier today!

    Here's another: newcalib.cal is empty after execution.

    Code:
    /* Program DIVIDE */
    
    #include <stdio.h>
    
    FILE* one;
    FILE* two;
    FILE* three;
    FILE* four;
    
    int main(void){
    
      float raw;
      float data;
      float delta;
      int dud;
    
      one = fopen("my113a_8_00.avg", "r");
      two = fopen("my113a_8_00C.dat", "r");
      three = fopen("differentials.dat", "r");
      four = fopen("newcalib.cal", "w");
    
      while(!EOF){
        fscanf(one,"%f", &raw);
        fscanf(two,"%f", &data);
        fscanf(three,"%d %f", &dud, &delta);
    
        printf("%f\n", delta);
        fprintf(four,"%f", raw/(delta+data));
      }
    
      fclose(one);
      fclose(two);
      fclose(three);
      fclose(four);
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Rather than exercizing proper loop control, you chose to just negate EOF outright. Well, EOF is a negative integer. So what do we end up with...
    Code:
    while( 0 ) {
       /* everything else */
    }
    No loop, no reading the files, no writing to files.

    > one = fopen("my113a_8_00.avg", "r");
    > two = fopen("my113a_8_00C.dat", "r");
    > three = fopen("differentials.dat", "r");
    > four = fopen("newcalib.cal", "w");

    Another significant problem is that you didn't make sure all of these FILE pointers were valid after the attempt to open a stream. If fopen didn't work, you don't know about it.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    37
    Quote Originally Posted by citizen View Post
    Rather than exercizing proper loop control


    Another significant problem is that you didn't make sure all of these FILE pointers were valid after the attempt to open a stream. If fopen didn't work, you don't know about it.
    if(!one)
    printf("Cannot open file");

    This should do..But still I wonder how you can open a stream to file without #include <fstream>
    Last edited by Sober; 05-04-2007 at 09:09 AM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Sober View Post
    This should do..But still I wonder how you can open a stream to file without #include <fstream>
    What are you talking about? This is C...

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by slippy View Post
    Code:
    fprintf(four,"%f", raw/(delta+data));
    Other people told you the real problem. But you need to also put a "\n" at the end of this fprintf() string. Otherwise all your output is going to be jammed together into one very long line.

  6. #6
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55
    Other people told you the real problem. But you need to also put a "\n" at the end of this fprintf() string. Otherwise all your output is going to be jammed together into one very long line.
    Yeah, I chaged the loop expression and added the `\n' earlier:

    Code:
    while(EOF != fscanf(three, "%d %f", &dud, &delta)){
    
    /* There's probably still room for improvement.... */
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Reading a file written by VB
    By nemaroller in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2002, 11:17 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM