Thread: Beginner File I/O Error

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Beginner File I/O Error

    I am trying to get C to go to a file, fscanf the information then bring it back and do a calculation. I have the .dat file in the same directory as the main.c file and the console window comes up when I build with either DevC++ or Code::Blocks but it only gives the message "press any key to continue". My instruction book was published in 98 so it might be something has changed but I think it is something I`m doing wrong since I`m just starting out. Can someone help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
      float hourly_rate;
      int   total_income;
      int   hours_per_week;
      float num_weeks;
    
      FILE * input_file;
      FILE * output_file;
       input_file = fopen("JOB.DAT", "r");
       output_file = fopen("INCOME.OUT", "w");
    
       fscanf(input_file, "%f", &hourly_rate);
       fscanf(input_file, "%d", &hours_per_week);
       fscanf(input_file, "%f", &num_weeks);
    
       total_income = hourly_rate  * hours_per_week * num_weeks;
    
       fprintf(output_file, "You will make $ %.2f in %d weeks this summer\n",
       total_income, num_weeks);
    
       fclose(input_file);
       fclose(output_file);
    
      system("PAUSE");
      return 0;
    }
    Thanks for any help.............

    Plain

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually tell it do display anything on the screen, so why would it? It just writes to a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Hi Quzah:

    Thank you for such a speedy reply.

    I thought the 6th line from the bottom with 'fprintf' would cause the file to be printed to the console. Can you explain my error?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     fprintf(output_file,
    That spot there tells it to print to a file. If you had put something like stdout there, it would display it on the standard output device (i.e.: your screen).


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Okay Quzah:

    I replaced 'fprintf' with 'stdout' and the compiler complained about something later in that same line of code not being a 'function'.


    Code:
    #include <stdio.h>
    
    int main()
    {
      float hourly_rate;
      int   total_income;
      int   hours_per_week;
      float num_weeks;
    
      FILE * input_file;
      FILE * output_file;
       input_file = fopen("JOB.DAT", "r");
       output_file = fopen("INCOME.OUT", "w");
    
       fscanf(input_file, "%f", &hourly_rate);
       fscanf(input_file, "%d", &hours_per_week);
       fscanf(input_file, "%f", &num_weeks);
    
       total_income = hourly_rate  * hours_per_week * num_weeks;
    
       stdout(output_file, "You will make $ %.2f in %d weeks this summer\n",
       total_income, num_weeks);
    
       fclose(input_file);
       fclose(output_file);
    
      system("PAUSE");
      return 0;
    }
    --------Then I put stdout in place of 'output_file' and got------
    Code:
    #include <stdio.h>
    
    int main()
    {
      float hourly_rate;
      int   total_income;
      int   hours_per_week;
      float num_weeks;
    
      FILE * input_file;
      FILE * output_file;
       input_file = fopen("JOB.DAT", "r");
       output_file = fopen("INCOME.OUT", "w");
    
       fscanf(input_file, "%f", &hourly_rate);
       fscanf(input_file, "%d", &hours_per_week);
       fscanf(input_file, "%f", &num_weeks);
    
       total_income = hourly_rate  * hours_per_week * num_weeks;
    
       fprintf(stdout,"You will make $ %.2f in %d weeks this summer\n",
       total_income, num_weeks);
    
       fclose(input_file);
       fclose(output_file);
    
      system("PAUSE");
      return 0;
    }
    The first threw me a compiler error "called object is not a function".

    The second time when I put stdout in the parenthesis the console does display the statement the only thing is it is filled with bogus numbers. I thought (obviously incorrectly) that the pointer had to stay in the statement but when I left it in the compiler whined again. Could you help by giving me a correct rendition of this little proggy? Maybe then I`ll be able to figure out what I`m supposed to do. I`ve been reading the last 3 hours in an attempt to find the right way to print a file result to the screen to no avail.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your "bogus numbers" have to do with whatever you're doing to your data.
    Code:
    #include<stdio.h>
    int main( void )
    {
        float f1 = 0.0, f2 = 0.0;
        int d = 0;
        FILE *in = fopen( "yourfilehere", "r" );
    
        if( in )
        {
            while( fscanf( in, "%f %d %f", &f1, &d, &f2 ) == 3 )
            {
                fprintf( stdout, "%f %d %f\n", f1, d, f2 );
            }
            fclose( in );
        }
    
        return 0;
    }
    Something close to that will get you started, depending on how exactly your file is formatted. Basically all you're doing here is reading a float, int, float, and displaying it on the screen.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    23
    Quzah!

    I thank you most heartily. I sometimes just cannot see these things. Have a good one!

    Plain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM