CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    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

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Simple question about dynamic concatenation

    Quote Originally Posted by Hypercomplex View Post
    ... 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.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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("&#37;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
  •  





Click Here to Expand Forum to Full Width

Featured