Solved

Int in filename

Posted on 2003-10-31
7
297 Views
Last Modified: 2010-04-01
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
   fstream f;
   
   int Number;

   f.open( *FILE PATH HERE* , ios::out);

I want to open a txt document in the c drive called "A__.txt"  (where the "__" is replaced with whatever value Number is).  My question is how I can combine "c:\\A" + Number + ".txt" to put into the file path.
0
Comment
Question by:HHB222002
7 Comments
 
LVL 5

Accepted Solution

by:
lwinkenb earned 25 total points
ID: 9662128
char szFile[32];
int number = 5;
sprintf(szFile,"c:\\A%i.txt",number);
f.open(szFile,ios::out);
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9662129
Use a variable for type char*(say char * str)

Initialise it with the initial string i.e. c:\........

Then using strcat, concatenate the value in Number into it and then concatenate ".txt. to the str

Then use the str in place of the filename in f.open
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9663409
Or, in a slightly more C++-ish manner you can use strstream:

#include <string>
#include <strstream>

...

     int Number = /* some value set */;

    strstream convert;
    convert << "A" << Number << ".txt";
 
    string fName;
    convert >> fName;

    // Now use fName as the name of your file and you are golden

-bcl
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:bcladd
ID: 9663410
Sorry- Forgot

using namespace std;

(or the name of the class is std::strstream)

-bcl
0
 

Author Comment

by:HHB222002
ID: 9664366
bcladd-

fName needs to be a char not a string though
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9664452
Use the c_str() method to return a pointer at a C-style string containing the information in a std::string:

ifstream input(fName.c_str());

-bcl
0
 

Author Comment

by:HHB222002
ID: 9666087
Thanks for your help.

I still can't get bcladd's way to work so I'm giving the points to lwinkenb
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now