Thread: beginner at c programming...how to manipulate multiple files in a single program?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Question beginner at c programming...how to manipulate multiple files in a single program?

    i need to write a program which reads three files containing data... this data is organized in the same manner in all of the files and only differs in its numbers...

    i wrote a program to read one file and manipulate its content, but i don't know how to get this program to read the other two files following this one in the exact same way..

    could i use a function to get this program to work?

    Code:
    #include <stdio.h>
    main ()
    {
        FILE *ontariofile;
        
        int beachnum, sampnum, s;         
        double samp,total, avg;
        
        ontariofile = fopen ("ontario.txt", "r");
        
        while (fscanf (ontariofile, "%d", &beachnum) != EOF)
        {
            total = 0;
            
            fscanf (ontariofile, "%d", &sampnum);
           
             
                
                    for (s = 1; s <= sampnum; ++s)
                    {
                        fscanf (ontariofile, "%lf", &samp);
                        total = total + samp;
                    }         
                    avg = total / sampnum;
                    if (avg > 50)
                    {
                        printf ("%d: open\n", beachnum);
                    }
                    else if (avg < 50)
                    {
                    printf ("%d: closed\n", beachnum);
                    }
                
                if (sampnum <3) 
                {
                    printf ("%d: insufficient data\n", beachnum);
                } 
    		                  
            
        }
            
    fclose (ontariofile);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You most definitely could and should use a function. But first:
    1. Always declare main to return int.
    2. Do some error checking. Check to make sure fopen worked, and check each usage of fscanf for an EOF, in case there's a problem reading the file or it's not properly formatted. It's a good habit to get in early, even if this is just for personal use. It will also help you find errors in your program.

    As for the function, simply rename your existing main function something like process_file and pass in the filename to process as a string. Then, your new main function will have a list of file (or take them as command line parameters) and call process_file on each one.

    Lastly, your program will print information about whether a beach is open or closed even if there's enough data. Your if (sampnum < 3) clause needs to be relocated.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Drop the fscanf altogether and use fgets.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    If you want to open additional files (as stated in your original post) then you could just reuse the same variable ('ontariofile' in this case). Obviously you would have to close the file before you use it. Alternatively you could open different file pointers if you wanted all three files open at the same time.

    Example:
    Code:
    ontariofile = fopen ("ontario_2.txt", "r");
    Where in Ontario are you? (if you don't mind me asking. I am there at the moment).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Compiling Multiple files in a single Project
    By pdwivedi in forum C Programming
    Replies: 2
    Last Post: 10-08-2007, 05:14 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM