473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to use a filename string with fstream?

I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp";
fstream InfoFile;
InfoFile.open(sFileInfo, fstream::app);
// >> do some i/o operations <<
InfoFile.close();
return 0;

How do I convert the filename string to the const char * that fstream
wants?

Jul 23 '05 #1
5 4261

"DellBoy" <pr**********@hotmail.com> schreef in bericht
news:11**********************@o13g2000cwo.googlegr oups.com...
I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp";
fstream InfoFile;
InfoFile.open(sFileInfo, fstream::app);
// >> do some i/o operations <<
InfoFile.close();
return 0;

How do I convert the filename string to the const char * that fstream
wants?


use sFileInfo.c_str()

Johan
Jul 23 '05 #2
sFileInfo.c_str()

DellBoy wrote:
I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp";
fstream InfoFile;
InfoFile.open(sFileInfo, fstream::app);
// >> do some i/o operations <<
InfoFile.close();
return 0;

How do I convert the filename string to the const char * that fstream
wants?

Jul 23 '05 #3
The method c_str() does this for you.
Example:
string sFileInfo = "MyFile.tmp";
fstream InfoFile;
InfoFile.open(sFileInfo, fstream::app);
InfoFile.open(sFileInfo.c_str(), fstream::app);
would be the replacement of the previous line.
// >> do some i/o operations <<
InfoFile.close();
return 0;


This should do it.

Manfred
Jul 23 '05 #4

"DellBoy" <pr**********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp";
fstream InfoFile;
InfoFile.open(sFileInfo, fstream::app);
// >> do some i/o operations <<
InfoFile.close();
return 0;

How do I convert the filename string to the const char * that fstream
wants?


Google for a reference on the std::string class. You return a pointer to a
const char* with its c_str() member function. Thats just one of a long list
of interesting and handy functions.

// test.cpp
#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string s_filename( "data.dat" );
std::ifstream ifs;

ifs.open( s_filename.c_str() );
if ( !ifs )
{
std::cout << "error while opening " << s_filename;
std::cout << std::endl;
return 0;
}

std::string s_buffer;
std::vector<std::string> vs;
while ( std::getline( ifs, s_buffer ) )
{
vs.push_back( s_buffer );
}

if ( !ifs.eof() ) // if reason of termination != eof
{
std::cout << "error while reading file.\n";
return 0;
}

// do something with the vector of strings

return 0;
}

Jul 23 '05 #5
Thank you all for the help, and the effort put in to explain how the
solution would work with my code. Really excellent response. DellBoy

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
1
by: Christopher Reeve | last post by:
Hi, I wonder if anyone could help me. I am moving accross from C to C++ and am writing a program that reads and writes data into a file. I am aware of the old C ways of doing it but am trying to...
2
by: Randy Yates | last post by:
Is there any way to get the filename with which a stream was opened? I don't see anything obvious in the stream API. -- % Randy Yates % "Ticket to the moon, flight leaves here...
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
by: Bonzol | last post by:
vb.net 1.1 Windows application Hey,, In a bit of a hurry,, does anyone know a way to turn a file in to a string,, such as a jpeg or pdf,, then turn that string back in to the original file? ...
7
by: pedagani | last post by:
Dear comp.lang.c++, I'm trying to read a file with very long filename using ifstream. Although, the file exists the file open for read fails. Is there a restriction on the size? I'm using winXP...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
2
by: jenniamia | last post by:
I need to make the string "myFile" into a byte and then put it in the database as a file/image, where later it needs to be pulled out and back into a string from the byte. I'm really having trouble...
10
by: drsmooth | last post by:
i need to open a file whose name i only know part of until runtime. it is based on a char value. the char represents gender and is either 'm' or ''f' i need to load from a text file called...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.