Solved

Int in filename

Posted on 2003-10-31
7
304 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unresolved External Symbols 3 158
Unable to start eclipse ? 17 174
c++, dynamic object by json 1 65
One named event, multiple event handlers 2 59
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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

751 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