Having issues with I/O (fprintf and such) new to programming

This is a discussion on Having issues with I/O (fprintf and such) new to programming within the C Programming forums, part of the General Programming Boards category; This is strange, my output to printf is exacty how i want it. But the output to a file is ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Having issues with I/O (fprintf and such) new to programming

    This is strange, my output to printf is exacty how i want it.
    But the output to a file is not what I expected. My instructor for class is horrible. (long story)
    the output to the regular printf is:
    Temp Avg High Low
    1173 46.00 100 -1

    fprintf is :
    (null)Temp/ Ave/ High/ Low
    (null)1173/ 0.00/ -1 / 0 (the slashes aren't there, I added those to section off the categories and numbers)

    I don't get why my file output is dorked up, can anyone point me in the write direction?

    Thanks for any help. Yes I am new to programming.
    my code is
    Code:
    #include<math.h>
    #include<stdio.h>
    #include<string.h>
    #define inp_file "C:\\Users\\John\\Desktop\\ehhh\\temps.txt"
    #define out_file "C:\\Users\\John\\Desktop\\ehhh\\averages.txt"
    #define debug 1
    
    int main(void) {
    
    
        FILE *inp, *out;
        double avg_temp;
        int High, Low, temp;
        
        inp = fopen(inp_file,"r");
        out = fopen(out_file,"w");
        
        if (debug) {
        if (inp == NULL || out == NULL) {
            printf("File does not exist\n");
    
    return(0); } }
        
        while (fscanf(inp,"%d\n", &temp) != EOF) {
            
            printf("%d\n", temp);            
            
                 }
                
                High = 100; //highest temp
                Low = -1; //lowest temp
                avg_temp = 1173 / 25;
                printf("Temp\t\tAve\t\tHigh\t\tLow\n");
                printf("1173\t\t%.2f\t\t%d\t\t%d\n", avg_temp, High, Low);
                fprintf(out,"%sTemp\t\tAve\t\tHigh\t\tLow\n");
                fprintf(out,"%s1173\t\t%.2f\t\t%d\t\t%d", avg_temp, High, Low);
            
            fclose(inp);
            fclose(out);
    
        return(0);
    }
    Last edited by jdouglas_usn; 11-16-2011 at 08:36 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Several things....

    1) checking file pointers is not a debugging function, it's a runtime safety function, so you can lose the if(debug) from line 18 entirely.

    2) Waiting for scanf() to return EOF (line 24) might cause the last line of the file to be read twice, once for the data and again for the EOF. You are better to use it like this...
    Code:
        while (fscanf(inp,"%d\n", &temp) > 0) 
           {
    This increases safety in 2 ways... first it will exit on garbaged data in the file and second it will exit when the last conversion fails or at EOF which should prevent the last line from being read twice.

    3) Why do your fprintf() statements in line 35 and 36 start with %s? That's probably what's killing our file writes...

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Thank you very much. Now I have to yell at my instructor for the %s. That is for strings, correct?
    overall it worked. Don't know why I didn't think of it.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jdouglas_usn View Post
    Thank you very much. Now I have to yell at my instructor for the %s. That is for strings, correct?
    overall it worked. Don't know why I didn't think of it.
    You clued me into it when you told me your display printing was fine but the file printing was messing up... First question, what's different?

    Also, if your instructor is THAT bad, you should probably report the situation to the dean or principal... Just think about how many careers a bad teacher can ruin.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    More likely you just misunderstood. You need the %s (or other format specifier) when you're printing the value of a variable. When you're printing a constant string you as the programmer created that isn't stored in a variable, you do not provide a format specifier. If you're not providing more than one argument to printf or fprintf, then you don't need a format specifier. A format specifier says "substitute the variable contained after into this location in the given string, printing as if it's this type".

    Also, if warnings were turned up in your compiler you likely would have been notified of this issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie issues programming C w/i Xcode
    By marcus314 in forum C Programming
    Replies: 5
    Last Post: 06-09-2010, 11:44 AM
  2. Replies: 3
    Last Post: 08-17-2009, 08:25 AM
  3. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  4. fprintf
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 11-16-2002, 05:58 PM
  5. C Programming issues
    By Vicky B in forum C Programming
    Replies: 13
    Last Post: 09-24-2001, 01:34 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