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 a student working on a project which requires me to create say 100 text files in C by naming the files in the format name001.txt , name002.txt , ... so on till 100.
    I want a logic to do this in loops..

    else i'd have to code it this way:

    FILE* fp[100];

    fp[0]=fopen("name001.txt","w");
    fp[1]=fopen("name002.txt","w");
    fp[2]=fopen("name003.txt","w");
    .
    .
    .
    till hundred??

    pls help..

    thanks..

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do you know how to code a loop?
    Mainframe assembler programmer by trade. C coder when I can.

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

    Unhappy @ dino

    ya i know to code a loop..
    i tried this code but its not working.. pls help..
    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);
    }
    }
    the programming is running into an error state..

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    A few errors:

    1) Yes, this line is no good:
    Code:
    add[0]=i;//is type conversion needed here ie. add[0]=(char)i; ???
    You can use sprintf() to convert i to a character string.

    2) You need to have enough room in array fileName[] to hold the largest built file name. Right now, it is only 5 bytes long ("name" followed by 0). Of you have 100 files, then fileName will need to be 3 bytes longer.

    3) If you initialize fileName to "name", as you are doing, then your logic will work the first time through the loop. But the second time through, you will append "2" to "name1", and you would get "name12". This is not what you want.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Please don't post here, but in stead here:
    Naming files in c

    As that one's more active.

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