Solved

Int in filename

Posted on 2003-10-31
7
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

617 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