Thread: file naming problem

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    39

    file naming problem

    I am not trolling and nor is this trying to complement the other thread I made.

    it is a genuine and real issue I am facing and worth starting a new thread - since it is very instructional.

    I just hope this does not become another hydra problem.

    So here is the issue. Suppose there is a very big file full of data. Total of 100,000 rows of data, in 3 columns.
    Now suppose those 10k rows actually are in 100 groups of data, each group/block is 1000 rows.


    So now i have to write a program that reads this one big file full of 100k rows of data, and then write it in 100 separate files.

    question is how to declare the file name - the crude way is to open 100 files, each named successively as 0.dat, 1.dat, 2. dat and so on.

    The other is to have some kind of running variable that will achieve this - however when i try -

    Code:
    fp2 = fopen (x".dat", "w");
    where x is the variable that stores the file number.

    However it is not working - can someone pls suggest -

    my guess it that i should start two loops

    the outer loop, i, going from 0 to 99
    the inner loop, j, going from 0 to 1000

    There has to be a pointer, say *x, that stores the value of i and then i should name the files like this -

    Code:
    fp2 = fopen (x".dat", "w");
    a tricky problem !!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not really.

    Code:
    char filename[8];
    sprintf(filename, "%03d.dat", ++x);
    fp2 = fopen(filename, "w");
    You will have to call something like this every time you want to open a new file but this totally generates "001.dat", "002.dat", "003.dat" and so on. Try not to have too many files open at once though, or fopen will start failing.
    Last edited by whiteflags; 04-07-2016 at 01:42 AM.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by but View Post
    a tricky problem !!
    A very simple problem, actually.
    Code:
    #include <stdio.h>
    
    int main(void) {
        char line[2000];
        FILE *fpin = fopen("inputfile", "r");
        for (int i = 0; i < 100; i++) {
            FILE *fpout;
            char filename[32];
            sprintf(filename, "%d.dat", i);  // or "%03.dat" to pad with zeroes
            fpout = fopen(filename, "w");
            for (int j = 0; j < 1000; j++) {
                fgets(line, sizeof line, fpin);
                fputs(line, fpout);
            }
            fclose(fpout);
        }
        fclose(fpin);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    hi thanks for the replies - let me see how much i figured out of this -

    Code:
    sprintf(filename, "%d.dat", i);  // this is the main line where the files are named as per the counter i
    fpout = fopen(filename, "w");  // opening this specially named file
    for(intj = 0; j < 1000; j++) 
    {
    fgets(line, sizeofline, fpin); // this reads a row at a time - but what if this has to be done like the last time - i need to read 5 columns, ignoring the first 2
    fputs(line, fpout); // i have to write in only the 3rd, 4th and 5th column
    So i got the previous "hydra" code nicely and even managed to understand how to have a "variable" file name, but I do not know how to combine the two - here too i have to ignore the first 2 lines, then read the 3rd, 4th and 5th column and print them out, 1000 sets of data at a time, but with this fancy naming system.

    Pls let me know if the comments i made are right or if i misunderstood your code.
    how to incorporate this new naming scheme with the code of the previous one -
    will the following line do -

    Code:
     fprintf(fpout, "%.5f %.5f %.5f\n", x, y, z);
    Last edited by but; 04-07-2016 at 01:37 PM.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by but View Post
    Pls let me know if the comments i made are right or if i misunderstood your code.
    Yes your comments are correct. And it should be quite simple for you to add the new functionality to your old code. If you're having trouble, post your attempt.

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    39
    sure - here it is
    Code:
     int i, j;
     double x, y, z;
     char line[4096];
     FILE *fp;
     fp = fopen ("abcd.gro", "r"); //  reading  
     for (j=1; j<3; j++)
         {
           FILE *fp2;
           char filename[32];
           sprintf(filename, "%d.dat",j);
           fp2 = fopen(filename, "w");
           fgets(line, sizeof line, fp);
           fgets(line, sizeof line, fp);
           for (i=0; i<13968; i++)
           {
            fgets(line, sizeof line, fp);
            if (sscanf(line, "%*f %*f %lf %lf %lf", &x, &y, &z) != 3)
            {
            printf("bad line: j=%d i=%d\[%s]\n", j, i, line);
            //return EXIT_FALIURE;
            }
            if (i%4 != 3)//i need to print all but every 4th line
                fprintf(fp2, "%.5f %.5f %.5f\n", x, y, z);
              }
         fgets(line, sizeof line, fp);
         fclose(fp2);
          }
    and it works - but i didnt entirely get what you did.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by but View Post
    and it works - but i didnt entirely get what you did.
    I'm glad it works. If you have any specific questions just ask.

    BTW, I notice you spelled EXIT_FAILURE incorrectly, which is presumably why you commented it out. It's defined in <stdlib.h>, so that must be included, too. On linux you can simply replace it with the number 1 if you want. The idea is that a non-zero return value indicates program failure. The return 0 at the end of main indicates program success.
    Last edited by algorism; 04-07-2016 at 04:52 PM.

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Sometimes I add a time stamp to the filename, but in this case a simple count would work as others have suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Naming an output file based on an input file name
    By mbxs3 in forum C Programming
    Replies: 4
    Last Post: 09-07-2013, 06:30 PM
  2. CGI dynamic naming of file download
    By Kennedy in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-18-2008, 01:33 PM
  3. DLL output file naming convension
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 08-31-2007, 12:24 PM
  4. naming a .txt file in c
    By nister in forum C Programming
    Replies: 11
    Last Post: 12-19-2002, 03:11 PM