Thread: Reading multiple files in C

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Reading multiple files in C

    I have the following problem: My program is supposed to read data from files to an array. The files are numbered: capture[1].bmp,capture[2].bmp and so on. There are 24 of those files, and I really don't want to do the same step 24 times. Is there a way to write a loop along the lines of

    Code:
    for (int i=1; i<25, i++);
    {
    fp=fopen("capture[i].bmp", "rb");
    
    fread(&ch,sizeof(ch),1,fp);
    	
    .....
    }
    In other words, is there a way to insert the "i" variable into the filename?



    Thank you for your insights.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    for (int i=1; i<25, i++)
    {
    char fn[100];
    sprintf(fn,"capture&#37;d.bmp", i );
    fp=fopen(fn, "rb");
    
    fread(&ch,sizeof(ch),1,fp);
    	
    .....
    }
    Use sprintf() to generate the filename.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If the files have the names as given in the original post, I suppose:
    Code:
    sprintf(fn,"capture[%d].bmp", i );
    would be what original poster wanted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    thanks for ur replies...............

    but if that is the case............where can i specify the path name

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In the sprintf statement, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Example :

    sprintf(fn,"/usr/home/capture%d.bmp".i)

    say you loop through that with i having a value of 0, 1, and 2.
    fn, then, in turn, will have :

    /usr/home/capture0.bmp
    /usr/home/capture1.bmp
    /usr/home/capture2.bmp

    That should get you what you want. Also, you may need to put in some escape characters like '\\' depending on what platform you're using.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    Thank you

    Thanks for ur reply..............it's working

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can you cut out the "ur" kiddie English and repeated dots - thanks.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    ok

    ok,thanks for your suggestion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. Opening Multiple Files in sequence
    By wujiajun in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2006, 08:47 PM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM