LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-15-2008, 10:17 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
C++ variable type trick...


I have three variables:

string className;
int number=92;
char fileName[10]="Math";

Is that possilbe I connect the int(92) and char[](Math) together?
And save "Math92.txt" into the string varialbe className?

Thanks!
 
Old 02-15-2008, 10:30 AM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
sprintf for first part

sprintf should be able to produce a string for you.

You'd have to look at string's member functions to see how string(s) are loaded. Perhaps, string objects allow you pointer access to their internal char buffer. At worst, you'd have an interim char buffer, sprintf the result of your text and integer to that, and then load your string variable from the buffer.
 
Old 02-15-2008, 11:07 AM   #3
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
you could this in many ways;
one way is to use sprintf(buf, "%s%d.txt", number, filename) where buf is a char buffer of appropriate size. then assign the contents of buf to className via assignment operator) className = buf
check http://www.cplusplus.com/reference/string/string/

another way is to use stringstream http://www.cplusplus.com/reference/i.../stringstream/
stringstream sstream;
sstream << number << filename << ".txt";
then copy the string buffer into className
className = sstream.str();

-Mustafa
 
Old 02-15-2008, 12:06 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'd go with snprintf to keep from overrunning your buffer.
Code:
#include <stdio.h>
#include <string.h>
#include <string>

int main()
{
    std::string className = "class";
    int number=92;
    char fileName[11]="Math";

    snprintf(fileName + strlen("Math"), 11 - strlen("Math"), "%i.txt", number);

    FILE *file = fopen(fileName, "a");

    if (!file) return 1;

    fprintf(file, "%s\n", className.c_str());

    fclose(file);

    return 0;
}
Don't forget space for the terminating \0!
ta0kira

PS Or even better:
Code:
    char fileName[256] = "";
    snprintf(fileName, 256, "Math%i.txt", number);

Last edited by ta0kira; 02-15-2008 at 12:11 PM.
 
Old 02-15-2008, 07:41 PM   #5
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
c'mon guys, the OP requested C++!


Code:
#include <sstream>

...
string className;
int number=92;
char fileName[10]="Math";

std::ostringstream ostr;

ostr << filename << number << ".txt";
className = ostr.str();
...
Sorry I didn't see elsheikhmh's post, but this is the ONLY way to do this in C++, not "another" way.

Last edited by JWPurple; 02-15-2008 at 07:45 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vim :gui trick and undo-trick dazdaz Linux - Software 3 09-10-2007 02:45 PM
understand variable type in shell script qrshat Programming 5 09-09-2006 02:12 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
make.conf in Gentoo linux, cpu type variable question.. bacium Linux - Software 1 05-11-2005 11:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration