Thread: Need help opening multiple files (c programming)

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

    Need help opening multiple files (c programming)

    Firstly I tried posting this question earlier and don't think it worked, so sorry if it did work eventually.

    So far I have been trying to complete this task without any outside help but unfortunately I have hit a brick wall. I have to open a series of 20 files and do some calculations with each file. I have managed to do open a single specified file and get the code to work and do all the calculations required. But I can't get it to open the files one by one and do the calculations for each

    any help would be greatly appreciated
    Thanks
    Last edited by awb8.; 11-28-2010 at 05:42 PM.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Maybe I'm missing something, but can't you just iterate over a list of files, i.e. and array of filenames?
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you can open/close/rw from a file, no difference for 20 files also.
    It seems like you don't need to open 20 files simultaneously.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Assuming your program is working correctly with the first file and all files are formated alike, this should be a simple matter. You have some choices...

    You can build an array (per msh's suggestion) with all the filenames in it.
    For more flexibility you could ask the user to enter the filenames for you.
    For "maximum smarts" you can itemize the files in the folder then process them in turn.

    In any event you will need to make your program loop until it's finished and you will have to replace the hard coded filename with a variable...
    Code:
    char fName[64];
    
     infile = fopen(fName, "r");

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    I have been told not to use an array, so I have been trying to avoid an array and would also like to keep the file short.

    I have 20 files with a random number of values in each, and need to do the same calculations to each file and print them at the end of the code. I've been trying to use a for loop to generate the file number e.g.
    Last edited by awb8.; 11-28-2010 at 05:43 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by awb8. View Post
    I have been told not to use an array, so I have been trying to avoid an array and would also like to keep the file short.

    I have 20 files with a random number of values in each, and need to do the same calculations to each file and print them at the end of the code. I've been trying to use a for loop to generate the file number e.g.

    Code:
     for (z=10; z==20; z++)
     {
         infile = fopen(("data-%d.txt", z) "r");
     }
     if (infile == NULL)
     {
        printf("Can't open data-%d.txt\n");
        system("pause");
        exit(1);
    }
    Ok that can work... but you need an extra step in there... fopen doesn't understand text formatting....

    Code:
    char fName[16];
    
     for (z=10; z<21; z++)    // assuming numbers from 10 to 20 inclusive
     { 
        sprintf(fName,"data-%d.txt",z);
        infile = fopen(fName "r");
        if (infile == NULL)
           {
             printf("Can't open %s \n",fName);
             system("pause");
             exit(1);
           }
    
    // process your data here
    
      }  // end of for loop
    In the interests of clarity you may want to make your file processing into a function call and call it from the spot marked but there's nothing wrong with doing it all in one big loop.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Thank you I will try that now. Can you just explain one thing for me.

    Why in your post earlier did you use:
    char fName[64]
    but in your last post you used
    char fName[16]

    I have never used the char function before and am a little confused by it

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by awb8. View Post
    Thank you I will try that now. Can you just explain one thing for me.

    Why in your post earlier did you use:
    char fName[64]
    but in your last post you used
    char fName[16]

    I have never used the char function before and am a little confused by it
    char is not a function, it's a Type... 1 signed Byte, typically 8 bits.

    In C strings are just arrays of characters. So declaring a char array of 16 elements, means you can store a 15 character string... That is 15 plus the trailing 0 that all C strings must have.

    This might help... http://www.cprogramming.com/tutorial/c/lesson9.html



    In my original post I had not seen the filename to know it's length. I used 64 on a "guesstimate" of the likely maximum. In the second post, where I did see the filename, I used 16 which is the 11 character name you specified plus a little extra.
    Last edited by CommonTater; 11-28-2010 at 10:34 AM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    OK I have got everything to ALMOST work. i can get the desired output for 1 of 2 things.

    I can get the correct calculation for either the correct amount of samples of all the files
    OR
    the correct amplitude of all the files.

    Can you see where my error is? or any advice on how to sort this problem?
    Last edited by awb8.; 11-28-2010 at 05:42 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why are you scanning the file twice?

    Your first while loop is going to leave the file pointer at the end. The second one is going to read nothing new. One of the two has to go...

    It seems to me you want....
    Code:
               fscanf(infile, "%f", &Total);
               for (i = 0; i < Total ; i ++)
    ...at the head of your processing loop.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    Firstly thank you all for the help you gave me, it was useful.
    I did manage to finish my task eventually

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by awb8. View Post
    Firstly thank you all for the help you gave me, it was useful.
    I did manage to finish my task eventually
    Congratulations.... care to share your final code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  2. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM

Tags for this Thread