Help creating multiple text files

This is a discussion on Help creating multiple text files within the C Programming forums, part of the General Programming Boards category; Hello all, I was trying to practice moving my programs into functions, so they are not only in the main() ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    Help creating multiple text files

    Hello all,

    I was trying to practice moving my programs into functions, so they are not only in the main() and also with creating files.

    I can create a single file with no problem. However, I could not get my program to succeed with creating multiple files.

    I want the program to be able to create say 4 text files each with a unique name and be concatenated with .txt at the end:

    text1.txt
    ..
    text4.txt

    Could someone point out where I am going wrong please?

    Here is the code I have done:

    Code:
    #include<stdio.h>
    
    int file_generator(int number);
    
    
    main()
    {
    
    int nrfiles = 4;
    
    
    file_generator(nrfiles);
    
    }
    
    
    int file_generator(int number)
    {
    
    FILE  *nf;
    int i=1;
    char newfile[10];
    for(i;i<=number;i++)
    {
    
    newfile[i] = "test";
    
    nf = fopen(newfile[i], "w");
    
    if (nf == NULL) {
      fprintf(stderr, "Can't open created file %s!\n",
              newfile);
      exit(1);
    }
    fclose(nf);
    }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you want to look at using sprintf() to combine strings with numbers?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    i am upset now.

    I ammended the above program

    so that"
    Code:
    nf = fopen(newfile[i], "w");
    is just:

    Code:
     nf = fopen(newfile, "w");
    I compiled and ran the program and it produced this output files:

    Code:
    ?0?????[?  ?0000??[?  000q?????  array    atm.c   file     matrix    test.txt
    ?00????[?  0000?????  00?q?????  array.c  conv    file.c   matrix.c
    ?000???[?  00000????  0??q?????
    i ran rm ?*

    and all my files were deleted
    :-(

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry to hear that.

    In unix (and Windows too) ? means "any character", and "*" means "any number of any character".

    Perhaps before you start creating files, just print the name you come up with?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    thanks matsp, I am trying that now. I mean to print out the names first.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    I managed to get the function to print out a diffierent name by using an array.

    I just cannot get the function to use this name now to create the file:

    Code:
    int file_generator(int number)
    {         
    char ch = 'a';
    FILE  *nf;
    int i=0; 
    char newfile[number];
    
    for(i;i<=number;i++)
      {   
       newfile[i] = ch;
       printf("%c\n", newfile[i]);
        
        
       nf = fopen(newfile[i], "w");
       if (nf == NULL) {
       fprintf(stderr, "Can't open created file %s!\n",
              newfile);
       exit(1);
       }
        
       if (nf == NULL) {
       fclose(nf);
       }
       ch = ch++;
      }

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    I kind of solved this with :

    Code:
    int file_generator(int number)
    {         
    char ch = 'a';
    FILE  *nf;
    int i=0; 
    char newfile[number];
    
    for(i;i<=number;i++)
      {   
       newfile[i] = ch ;
       printf("%c\n", newfile[i]);
        
       nf = fopen(newfile, "w");
       if (nf == NULL) {
       fprintf(stderr, "Can't open created file %s!\n",
              newfile[i]);
       exit(1); 
       }
        
       if (nf == NULL) {
       fclose(nf);
       } 
    
       ch = ch++;
      }
    }
    but it creates files like this:
    Code:
    :~/Programming$ ls
    a  ab  abc  abcd  abcde  file  file.c
    i am tired so I will go to bed

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, first some petty details:
    Your string is too short. If you use i <= number, you will need to have a array that is number+2 characters long.

    Code:
    ch = ch++;
    Just ch++ is fine - it does what you need. The "ch =" part is completely unnecessary.

    Code:
       if (nf == NULL) {
       fclose(nf);
       }
    Would it not be a better idea to close the file if it WAS success?

    Now, what do you actually want to do? Just create files called a, b, c, d, e? If so, you need to set the FIRST character of your array to whatever you want to call the file, and the second entry to 0. That way, you have a string of 1 character. You are currently setting position "i" in the array to the letter - that is not right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    i am tired so I will go to bed
    Hey, sleepy head

    Code:
    #include <stdio.h>
    
    void filenumber(void);
    
    int main(void)  {
       int i;
    
       printf("\n\n Our Files Are: \n");
       for(i = 0; i < 2; i++)
          filenumber();
    
    
       printf("\n\n\t\t\t     press enter when ready ");
       i = getchar();
       return 0;
    }
    void filenumber(void)  
    {         
       FILE  *nf;
       char filename[10] = {"Text"};
       char ext[5] = {".txt"}
       char afilenumber[3] = {'\0'}; 
       int i=0, j; 
    
       static int filenumber = 0;
    
       for(i = 0; i < 4; i++)  {
          filenumber++;    
          itoa(filenumber, afilenumber, 10);
          strcat(filename, afilenumber);
          strcat(filename, ext);
       
          if((nf = fopen(filename, "w")) == NULL)  {
             fprintf(stderr, "Can't open %s file!\n", filename);
             exit(1); 
          }
          
          printf("\t The new file name is %s \n", filename);
          
          filename[4] = '\0';
          fclose(nf);
       }
       return 0;
    }
    Edit: changed to make text1.txt, text2.txt, text3.txt, etc., filenames (but I haven't tried this out yet ).
    Last edited by Adak; 03-27-2009 at 03:35 PM.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thanks matsp for you valuable input. And Adak, what a nice surprise to see your effort!

    I will have to wait till this evening to try these. Will let you both know what happens.

    Again, much thanks both of you.
    Last edited by Tom Bombadil; 03-27-2009 at 04:40 AM. Reason: spelling mistakes

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Hello there. I tried to do as you said matsp but I didn't manage. I am not sure how to do what you say:

    Code:
    you need to set the FIRST character of your array to whatever you want to call the file, and the second entry to 0. That way, you have a string of 1 character. You are currently setting position "i" in the array to the letter - that is not right.
    I tried to do it a different way using pointers, but I get the same result:

    Code:
    int file_generator(int number)
    {
    
    char newfile[6] = {"TomB"};
    
    char ch = 'a';
    char *nfP;
    nfP = &newfile;
    
    FILE  *nf;
    int i=0;
    
    for(i;i<=number;i++)
      {
       printf("%c\n", *nfP);
    
       nf = fopen(nfP, "w");
       nfP++;
        if (nf == NULL) {
         fprintf(stderr, "Can't open created file %s!\n",
              nfP);
       exit(1);
       }
    
       if (nf == NULL) {
       fclose(nf);
       }
      }
    }
    which yields:

    Code:
    B  file  file.c  mB  omB  TomB
    I feel like I am digging my own TomB here!

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What file names are you trying to create?

    I changed my program to make text1.txt, text2.txt, etc., filenames (untested for that part, however).
    Last edited by Adak; 03-27-2009 at 03:36 PM.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    For the above program that I did to just take the array contents "TomB" and create from it files:

    T
    o
    m
    B

    instead of the result stated above. I couldn't solve it :-(
    I think it is slightly too much for me at this limited stage in my C experience.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me mess wit it - wit a hammer, if necessary!

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main()  {
       int i;
       char newfile[6] = {"TomB"};
       char *ch;
    
       FILE  *nf;
    
       i = 0;
       while(newfile[i]) {
          ch = &newfile[i];
          nf = fopen(ch, "w");
    
          printf("\n%c ", *ch);
          i++;
          if (nf == NULL) {
             fprintf(stderr, "Can't open created file %c!\n", ch);
             exit(1);
          }
       //if nf == NULL the file was never opened, so no need to close it :)
       }
       i = getchar();
       return 0;
    }
    Try that.
    Last edited by Adak; 03-27-2009 at 04:26 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Creating multiple files with string as filename
    By jmayer in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2008, 08:55 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21