Thread: creating a new file but with automatically generated names

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    creating a new file but with automatically generated names

    hi guys, its me again!!

    i managed to create a file with a name of "1.txt"

    what i want is to add 10 digits(which i can do), close the file and open a new one, do the same and so on.

    up to a max of of 23 files.

    the names of the files must be sequentially rising i.e.

    1.txt
    2.txt
    3.txt
    ........
    23.txt


    this is the code, i have so far

    Code:
    #include <stdio.h>
    
    
    #define MAX 10
    
    int main()
    {
        FILE *f;
        int x;
        f=fopen("out.txt","w");
    		if fopen="out.txt"
    		f=fopen("out1.txt","w");	
        if (!f)
            return 1;
        for(x=1; x<=MAX; x++)
            fprintf(f,"%d\n",x);
        fclose(f);
        return 0;
    }
    could anyone give me a hint!?

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Make a loop. In the loop, do the following each iteration:

    - Put together a file name, consisting of an index number( 1, 2, 3, etc...) and the type.
    - Create the file, open it, write to it, and close it.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Theres got to be an easier way than this!!!

    Code:
    #include <stdio.h>
    
    
    #define MAX 10
    
    int main()
    {
        FILE *f;
        int x;
        f=fopen("1.txt","w");
    	if (!f)
            return 1;
        for(x=1; x<=MAX; x++)
            fprintf(f,"%d\n",x);
        fclose(f);
     
    	FILE *g;
        int y;
        f=fopen("2.txt","w");
    	if (!f)
            return 1;
        for(y=1; y<=MAX; y++)
            fprintf(f,"%d\n",y);
        fclose(f);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks sean

    now, what if i wanted to add external data into each file. and had to wait until that file was completed before moving on.

    i would know if the file is completed, i have already done the flags for that.

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    untested code:

    Code:
    char buf[BUFSIZ];
    FILE *fp;
    int i, j;
    
    for (i=0; i<MAX; i++){
        sprintf(buf, "%d.out", i+1);
        if ((fp = fopen(buf, "w")) == NULL){
            fprintf(stderr, "Can't open %s!\n", buf);
            continue;
        }
        for (j=0; j<MAX; j++)
            fprintf(fp, "%d", j);
        fclose(fp);
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    There is. You haven't implemented what I said in my previous post. You're not putting together any filenames - you're just giving them to the program. Have a number that increase each time (though you'll want to use the ASCII value of the character "1", then add 1 to get the character "2"). Concatenate the file extension to this character and use that string as your filename. Open it, write to it, close it, and then the loop should start again, incrementing the number. I'm not going to write the code for you.

    now, what if i wanted to add external data into each file. and had to wait until that file was completed before moving on.
    What external data is this? Unless you're using multiple threads in your program (and you're not, from what I can see) then your program won't move onto the next line until the current one is finished.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    what i need to do, which i should have explained is to get data from a com port and put it into files. every preamble has a character # so i can easily recognise when to open a new file and start writing.

    thats what i wanted to do.

    the previous code was my own attempt at it

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by mitakeet
    untested code:

    Code:
    char buf[BUFSIZ];
    FILE *fp;
    int i, j;
    
    for (i=0; i<MAX; i++){
        sprintf(buf, "%d.out", i+1);
        if ((fp = fopen(buf, "w")) == NULL){
            fprintf(stderr, "Can't open %s!\n", buf);
            continue;
        }
        for (j=0; j<MAX; j++)
            fprintf(fp, "%d", j);
        fclose(fp);
    }

    tried that code but get some errors, looking into it now.

    error:

    Code:
    --------------------Configuration: simple file opening - Win32 Debug--------------------
    Linking...
    testcrap.obj : error LNK2005: _main already defined in simple file opening.obj
    Debug/simple file opening.exe : fatal error LNK1169: one or more multiply defined symbols found
    Error executing link.exe.
    
    simple file opening.exe - 2 error(s), 0 warning(s)

  9. #9
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You are linking two files that have a main() routine. Start a new project.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by mitakeet
    You are linking two files that have a main() routine. Start a new project.

    thanks alot guys.

    you both are stars!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM