0

I'm relatively new to C, coming from python mainly and I want to be able to create a new variable as a string to use the function strcat() based of an integer. For example, if each time I loop through it and increment an integer, 1, a new variable that is a string/char is now "1", and 2, "2" and so on, so that when using strcat(dest, value), it makes a string for example called: (1st loop) "TEST1.txt", (2nd loop) "TEST2.txt" and so on. If this doesn't really make sense, the best way I can describe this is in python, to achieve the exact same thing, say:

a = 1
while True:
    file = open("Test" + str(a) + ".txt", "w")
    file.close()
    a += 1
    if a == 10:
        break

I know how to do it with strings, for example if a isn't an integer, from the python code to do the "Test" + str(a) in c, i can do:

char* a = "test";
char* b = "1";
strcat(a, b);

if i print that i would get "test1", but I need it to be an integer first then a string so that i can increment it, thanks

2 Answers2

0

There is no great sense to use strcat in this case. Just write

char file_name[] = "test0.txt";

do
{
    ++file_name[4];

    FILE *fp = fopen( file_name , "w")
    //...
    fclose( fp )
} while ( file_name[4] != '9' );

Or you can usethe function sprintf as for example

#include <stdio.h>

int main(void) 
{
    char name[10];
    int i = 0;

    do
    {
        sprintf( name, "%s%1d%s", "Test", ++i, ".txt" );
        printf( "%s\n", name );
    } while ( i != 9 );

    return 0;
}

The program output is

Test1.txt
Test2.txt
Test3.txt
Test4.txt
Test5.txt
Test6.txt
Test7.txt
Test8.txt
Test9.txt
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think you meant `fopen` and `fclose` – Irelia Oct 07 '19 at 11:02
  • just wandering with this though, when you do char filename[], how large of a string can you type, because i would have thought you would have to specify the size of the array like: char filename[50] = "test0.txt"; –  Oct 07 '19 at 11:16
  • @404TeamNotFound The size of the array in this case is determinated from the number of initializers that is from the number of characters in the string literal including its terminating zero. – Vlad from Moscow Oct 07 '19 at 11:20
  • lol it really shows i have alot to learn with c, but im assuming that that means the limit is determined by how long the string is that i type in or whatnot, sort of like python how integers aren't restricted but only use more memory when necessary? Eh idk, but I also don't know what an initializer, the string literal and terminating zero mean but thanks for the reply –  Oct 07 '19 at 11:24
  • `++file_name[4]` is needlessly brittle (and in several respects). – Steve Summit Oct 07 '19 at 13:04
  • @SteveSummit It is a valid expression provided that the range of the integer between 0 and 9 (more precisely between '0' and '9'). So there is nothing wrong. – Vlad from Moscow Oct 07 '19 at 13:20
  • As I said, it is needlessly brittle. It will break if more than 10 files are ever needed. If will break if someone changes `file_name` to `filename0.txt`. It's the kind of thing C hackers did in the 1970s and 1980s. It is not (IMO) a technique to be recommending in 2019. – Steve Summit Oct 07 '19 at 13:23
  • @SteveSummit In the code snippet shown in the question there are considered only numbers in the range [0, 10). So your comment is irrelative. – Vlad from Moscow Oct 07 '19 at 13:24
  • I didn't say it was broken! Of course it works -- today. But it is brittle, and needlessly so. – Steve Summit Oct 07 '19 at 13:25
-2

In the standard library stdlib.h you can use the function itoa to return a string given a integer. (For reference, you can do the reverse using atoi)

char *  itoa ( int value, char * str, int base );

I don't have a C reference to hand on the function, but some links to get you started are either here or here.

James C
  • 1
  • 2