Thread: Naming files in c

  1. #1
    Registered User sandeep080's Avatar
    Join Date
    Apr 2010
    Posts
    17

    Question Naming files in c

    Hi,

    I'm student working on a project which requires me to create text files using C program. My problem is that i want to create say a 100 text files which has to be named in the following format: name001.txt , name002.txt , and so on till hundred..

    I'm not finding a logic by which I can name theSE files in this way using loops..
    I'm sure there is some way.. else we need to write 100 statements to open EACH file. eg:

    FILE* fp[100];
    fp[0]=fopen("name001","w");
    fp[1]=fopen("name002","w");
    .
    .
    .
    till 100??

    pls help???

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why the double post? But yes, you should use a for loop for this.

  3. #3
    Registered User sandeep080's Avatar
    Join Date
    Apr 2010
    Posts
    17

    Sorry

    *sorry for the double post.. i'm a first time user..

    I need the logic of renaming the text file everytime the loop is executed?? how do you do this??

    thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Work out how to write an integer to a string and looping becomes easy. sprintf() supports relevant format specifiers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    See if this helps your thinking along:

    You have a char fileName array. You declare it with the base name you want:
    Code:
    char fileName[] = {"name"};
    char add[4] = {'\0'};
    Then in your loop you use itoa to change your iterator, into a string, and assign it to "add[]".

    Now strcat() add onto the end of fileName, and you can then open the file:

    Code:
    fp = fopen(fileName, "wt");
    Just like normal, except don't put double quotation marks around fileName.

    sprintf() is a good way also,
    Last edited by Adak; 04-28-2010 at 05:34 AM.

  6. #6
    Registered User sandeep080's Avatar
    Join Date
    Apr 2010
    Posts
    17
    @Adak..

    as per your reccomendation i tried this code(below).. but the program is running into an error state.. and a pop up appears saying, "... has encountered a problem and needs to close. We are sorry for the inconvenience".. i'm worried about the type conversion in add[0]=i;
    anyway thanks again..

    Code:
    //file name numbering program
    #include<stdio.h>
    #include<string.h>
    void main()
    {
    int i;
    FILE* fp;
    char fileName[]={"name"};
    char add[4]={'\0'};
    char s[]={"C PROGRAMMING"};//sample text that has to be written into the file
    for(i=0;i<10;i++)
    {
    add[0]=i;//is type conversion needed here ie. add[0]=(char)i; ???
    printf("%c",add[0]);
    strcat(fileName,add); //concatenating to change file name everytime
    fp=fopen(fileName,"wt");
    fprintf(fp,"%s",s);
    fclose(fp);
    }
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you define fileName as only being four characters long, you probably won't be able to add much to it. (That's a bug in Adak's original too, I'm afraid.) Your printf is just plain broken, though. You want to print to a string, which printf doesn't do.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to size fileName a bit bigger - my fault. Below is your program, with just a few tweaks.


    Quote Originally Posted by sandeep080 View Post
    @Adak..

    as per your reccomendation i tried this code(below).. but the program is running into an error state.. and a pop up appears saying, "... has encountered a problem and needs to close. We are sorry for the inconvenience".. i'm worried about the type conversion in add[0]=i;
    anyway thanks again..
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //void main() let's use int main() instead!
    int main()
    {
        int i;
        FILE* fp;
        char fileName[8]={"name"};  //handles up to 3 numbers being appended
        char add[4]={'\0'};
        char s[]={"C PROGRAMMING"};//sample text that has to be written into the file
    
        printf("\n\n");
        for(i=0;i<10;i++)
        {
            add[0]= (char) (i + '0'); //type conversion can help, 
            printf("%c",add[0]);
            strcat(fileName,add); //concatenating to change file name everytime
            fp=fopen(fileName,"wt");
            fprintf(fp,"%s",s);
            fclose(fp);
            fileName[4] = '\0'; //resets fileName for the next number
        }
    
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar();
      return 0;
    }
    You're welcome!
    Last edited by Adak; 04-28-2010 at 09:08 AM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Adak View Post
    You need to size fileName a bit bigger - my fault. Below is your program, with just a few tweaks.
    And yet, still, completely wrong :P.

    Use sprintf.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by EVOEx View Post
    And yet, still, completely wrong :P.

    Use sprintf.
    I have already posted that you can use sprintf() to do this. I just chose another way. The program as posted, creates 10 files with consecutively numbered filenames, and writes the string in each one of them.

    That doesn't fit my definition of "wrong".

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Adak View Post
    That doesn't fit my definition of "wrong".
    This is true. hack != wrong.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Novice
    Join Date
    Jul 2009
    Posts
    568
    That's nice. But it will crash and burn if you go over 10 files.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Adak View Post
    I have already posted that you can use sprintf() to do this. I just chose another way. The program as posted, creates 10 files with consecutively numbered filenames, and writes the string in each one of them.

    That doesn't fit my definition of "wrong".
    Not in "wrong" as in that it doesn't work. But definitely, undoubtedly "wrong" as in that it's NOT the way to go.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes, it will go weird after 10 files, but that is all the OP put into his for loop, and this was the logic he chose.

    I suggested using itoa(), but the OP wanted it that way.

    Code:
        for(i=0;i<10;i++)
        {
            //add[0]= (char) (i + '0'); //type conversion can help, 
            itoa(i, add, 10);  
            printf("%c",add[0]);
            strcat(fileName,add); //concatenating to change file name everytime
            fp=fopen(fileName,"wt");
            fprintf(fp,"%s",s);
            fclose(fp);
            fileName[4] = '\0'; //resets fileName for the next number
        }
    Sprintf() is a better choice, but itoa() was the first thing that came to my mind. I leave that wrinkle, as an exercise for the OP.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by EVOEx View Post
    Not in "wrong" as in that it doesn't work. But definitely, undoubtedly "wrong" as in that it's NOT the way to go.
    Probably why I didn't suggest the cast method, but the OP wanted it.

    Nothing wrong with the OP experimenting and finding out some logic doesn't work, or works only in a limited range.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2009, 06:45 AM
  2. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM