Question on strings

This is a discussion on Question on strings within the C Programming forums, part of the General Programming Boards category; Hi, I'm a begginer in C (learning it for fun) and I have a question regarding strings. I programmed a ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Question on strings

    Hi,

    I'm a begginer in C (learning it for fun) and I have a question regarding strings.

    I programmed a simple 'extractor' for a file format and I want each of my files to be named sequentially as FILE00.ext, FILE01.ext, etc.

    Here is the code snippet that write the files:

    Code:
    for (i = 0; i < num_of_files; i++)   {
        
        fseek(input, (file_offset[i] + start_offset), SEEK_SET);
        
        FILE *ripped_file = fopen( filename , "w+b"); 
        
        for ( j = 0; j < ( file_offset[i+1] - file_offset[i] ); j++)    {
            
            ch = getc(input);
            putc(ch, ripped_file);
            
            }
        
        fclose(ripped_file);
        
        }
    Everything works fine, but how can I make the "filename" be incremented each time? I thought of doing some like counter++ (with an integer) but I'm not sure how can I append that to the filename. Can someone give an actual example of this?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    ....
    char filename[10]; //the name, like FILE in your example, or anything else you would like
    char varStr[7]; // the number followed by .ext
    char newFilename[17]; //the filename you want
    
    ...
    
    strcpy(varStr, "00.ext");  //varStr is "00.ext"
    newFilename = strcat(filename, varStr); //this appends the varStr to the filename
    
    ...
    //This creates FILE00.ext, FILE01.ext etc etc
    FILE * fd[5];
    for (int i=0; i<5; ++i) {   
       newFilename = strcat(filename, varStr);
       fd[i] = fopen(newFilename, "w");
       varStr[1]++;
    }
    Notice that varStr[1] = '0' at the beggining. If you increase it it will be '1' (not 1, but the char '1'). And so on. At the tenth file of course you have to set varStr[1] = '0' and varStr[0] = '1'. Last note, every string is null-terminated. If you make that sure then it should work for a size less than the maximum size of the array

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    Thank you, that's very helpful .

    I thought the "++" increment could be only used for integers, so I had the wrong idea that I had to use some sort of typecast. It's much simpler, thank you again.

    Edit: Ops, there a few problems with your solution. After 9, I start to get random ASCII chars instead of a number, so I guess this is not quite what I want.
    Last edited by papagaio; 09-18-2008 at 05:04 PM. Reason: tested the code and found some problems

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Code:
    int filenum = 0;
    char filename[100];
    
    // Repeat this
    sprintf(filename, "my file&#37;i.txt", filenum);
    filenum++;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 05:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-14-2005, 11:14 PM

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