Thread: Need help opening multiple files (c programming)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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

  4. #4
    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.

  5. #5
    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.

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