-
February 10th, 2012, 03:54 PM
#1
Simple question about dynamic concatenation
Hi,
So I have a very simple question (I'm beginner ) about concatenation with the strcat function :
I have a program in C, I want to export a .txt file containning results, I have to put the location in a char[], I have something like that =
"
.......
GetCurrentDirectory(256, file);
strcat(file,"\\log.txt");
....
"
and it works, no problem, but I'd like to export many log files so I need dynamic concatenation, something like that :
"
.......
int x = 5;
GetCurrentDirectory(256, file);
strcat(file,"\\logx.txt");
....
"
and having a log5.txt in my file, into not erasing log4.txt for exemple ... I searched with the sprintf function for cast, but I have a problem, if I do
"
.......
GetCurrentDirectory(256, fichier);
strcat(file,"\\log");
strcat(file,".txt");
....
"
It doesn't work, I don't understand why ... I want to do strcat(file,"\\log" +x+".txt" ); like in java for exemple but I don't find how to write it ... someone knows it plz ?? =)
thx
-
February 10th, 2012, 04:21 PM
#2
Re: Simple question about dynamic concatenation
Originally Posted by Hypercomplex
... I searched with the sprintf function for cast, but I have a problem, if I do
Why do you say sprintf had a problem, then show code that has no sprintf calls??
sprintf should work. If not then post the sprintf code you tried.
But ostringstream is a better alternative to sprintf.
-
February 10th, 2012, 04:59 PM
#3
Re: Simple question about dynamic concatenation
strcat(file,"\\log");
strcat(file,".txt");
....
"
It doesn't work, I don't understand why ...
It works just fine:
Code:
#include <stdio.h>
int main()
{
char file[260] = "C:\\some_path";
strcat(file,"\\log");
strcat(file,".txt");
printf("%s\n", file);
return 0;
}
Code:
D:\Temp\37>37.exe
C:\some_path\log.txt
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|