Thread: help plz: opening mutiple files in c

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    help plz: opening mutiple files in c

    i have a bunch of files to open as 1,2,3......1000

    i can open a file as:
    ifstream indata;
    indata.open("1");

    but i dont want to open all of them manually,
    is there any way to open like -

    for(int i=1;i<1000;i++)
    indata.open(i);

    plz tell me asap...thanks...

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    map<string,ifstream> filehash;
    ifstream cur;
    for (i=0; i<100; i++) {
        cur.open("name_of_file",ios::in);
        if (cur.fail()) cerr << "Can't open....";
        filehash["name_of_file"] = cur;
    }
    There may be a limit set by your OS on the number of open files one process can have.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    the problem is i have to write this statement 1000 times (for 1000 files)
    cur.open("name_of_file",ios::in);

    like cur.open("1",ios::in);
    cur.open("2",ios::in);
    cur.open("3",ios::in);
    ........and so on

    If we write it in a loop:

    for(i=1;i<1000;i++)
    cur.open(i,ios::in);

    it will give error because i is integer & it wants string there.
    If we try to type cast it, it will convert integer value into ascii which we dont want.
    Is there any other solution?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The best way to do this, if all these files are in the same directory, is open the directory -- with opendir() -- read the file names into a vector<string> -- with readdir(), etc -- then you can use that vector in the for loop.

    If you are too lazy for that, tho, and all the files are literally called "1" "2", etc, just create a vector of the filenames:

    Code:
    	vector<string> fnames;
    	int i;
    	char tmp[8];
    	for (i=0;i<100;i++) {
    		sprintf(tmp,"%d",i);
    		fnames.push_back(tmp);
    		cout << fnames[i] <<endl;  // verify
    	}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 02:23 PM
  2. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 06:52 AM
  3. Replies: 2
    Last Post: 11-05-2001, 02:02 PM
  4. opening .doc files in unix through C program
    By indira78 in forum Linux Programming
    Replies: 4
    Last Post: 11-05-2001, 12:57 PM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 07:06 AM