reading multiple files

This is a discussion on reading multiple files within the C Programming forums, part of the General Programming Boards category; hallo, i am in search of solution for the following problem . i have nearly 100 files and i need ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    Post reading multiple files

    hallo,
    i am in search of solution for the following problem . i have nearly 100 files and i need to read them. how is it possible.
    bye
    sudhir

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Something like this?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       for (i = 1; i <= 100; i++)
       {
          FILE * pFile;
          char filename[20];
    
          sprintf(filename, "myfile%d.dat", i);
          if ((pFile = fopen(filename, "r")) == NULL)
          {
             printf("could not open file %s\n", filename);
             exit(1);
          }
    
          /* .... do whatever you want to do with the file .... */
    
          fclose(pFile);
       }
    
       return 0;
    }
    (assumes that your files are named sensibly eg myfile1.dat, myfile2.dat, myfile3.dat ... etc.)
    DavT
    -----------------------------------------------

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or if you don't have any system in the filenames, use a lookup list:
    Code:
    #define NrOfFiles 6
    
    const char* FileName[] = {
    "MyFile1.txt",
    "MyFile2.txt",
    "OurFile.txt",
    "Banana.txt",
    "YourFile.txt",
    "EveryonesFile.txt"};
    
    ...
    
    FILE* ReadFile;
    
    for(int i = 0; i < NrOfFiles; i++)
    {
       ReadFile = fopen(FileName[i], "rb");
    
       ...
    
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    thank u guys the code is working
    bye
    sudhir

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, 10: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, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21