#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    How to read multiple files?


    I want to read about 154 files with names as in1 to in154.
    I want to open each file, read the data, sort & then store in respective 154 files, like out1-out154.
    I'm using following code

    char fileName[] = "in1.txt";
    ifstream inFile;
    inFile.open(fileName);

    how can i read filenames with some loop?
    please help as soon as possible.
    Thanx.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Something like this maybe?
    Code:
    #include <cstdio>
    
    char fileName[100];
    for (int i=1; i <= 154; i++) {
        sprintf(fileName, "in%d.txt", i);
        ifstream inFile;
        inFile.open(fileName);
    }
  4. #3
  5. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Here's a related question:

    Is there a defined limit on the number of file handles for windows systems, like there was in DOS?

    You should be careful with how many files you open at the same time, especially on a system that has limited file handles that runs more than one application at the same time.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    Thanks


    Thanks for your help.
    Ketaki.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    code help


    previous codes won`t work
    in order to change the filenames you should create cycle
    to generate the number from 1 to 154, the next step is ot convert this integer numbers to strings and to insert these strings as substrings in the filename variable after position 1;
    ex.
    char dynvar[20];
    char filename[20];
    FILE *fp;
    strcpy(filename,"in.txt");
    for (i=0;i<155;++i) {
    dynvar = itos(i);
    /* i`m not quite sure for the correct function check yourMSDN libraries for the correct one
    */
    // here you should create function for inserting the substring
    // these could be done with memove(); function
    if (fp = fopen(dynvar,"r+b") == NULL) {
    printf("\nError opening file %s",filename");
    exit(1);
    }
    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247

    Re: code help


    Originally posted by HubGoblin
    >> previous codes won`t work
    With all due respect, you're definitely smoking some interesting ceremonial chemicals.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    11
    Rep Power
    0

    I'm done !!


    Thanks for your time.

IMN logo majestic logo threadwatch logo seochat tools logo