473,807 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s FileInfo, 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 4282

"DellBoy" <pr**********@h otmail.com> schreef in bericht
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp ";
fstream InfoFile;
InfoFile.open(s FileInfo, 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(s FileInfo, 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(s FileInfo.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**********@h otmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I would like to be able to do something like this:

string sFileInfo = "MyFile.tmp ";
fstream InfoFile;
InfoFile.open(s FileInfo, 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_st r() );
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
11437
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 ifstream and fstream as well, but I'm typing on the fly) yes, I know the syntax may be off... class onamedfstream : public ofstream {
1
4010
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 use the C++ functions which don't seem to be working propperly. I need to open the file for reading and writing because I want to search for a possition in a file called settings.dat and then write a number after a certain chatacter. However when...
2
1516
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 today %% Fuquay-Varina, NC % from Satellite 2" %%% 919-577-9882 % 'Ticket To The Moon' %%%% <yates@ieee.org> % *Time*, Electric Light Orchestra http://home.earthlink.net/~yatescr
10
18045
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
3373
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? Cheers
7
4943
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 with VS2005. Please advice. Thank you.
12
4270
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 N), I generate these output data files. After running the program once for a given value of N (say N equal to 1) and when the programme is finished, I manually append the generated filename with the value of N, such as 'Voltage_N1.dat',...
2
2317
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 gettting it back into a string afterwards, but I don't even know if the data is being put in the database with correct values. Any suggestions or pointers would be much appreciated. Thanks! (I posted the main section I'm having trouble with, but I...
10
2538
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 names_m.txt or names_f.txt but i don't know until runtime. i tried this but it didnt quite work... char fileName = {"/data/names_"};
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.