Connecting Tech Pros Worldwide Help | Site Map

How to use a filename string with fstream?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:52 AM
DellBoy
Guest
 
Posts: n/a
Default 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?


  #2  
Old July 23rd, 2005, 04:52 AM
Johan
Guest
 
Posts: n/a
Default Re: How to use a filename string with fstream?


"DellBoy" <professorcad@hotmail.com> schreef in bericht
news:1117281610.334518.250070@o13g2000cwo.googlegr oups.com...[color=blue]
>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?
>[/color]

use sFileInfo.c_str()

Johan


  #3  
Old July 23rd, 2005, 04:52 AM
Daniel Etzold
Guest
 
Posts: n/a
Default Re: How to use a filename string with fstream?

sFileInfo.c_str()

DellBoy wrote:[color=blue]
> 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?
>[/color]
  #4  
Old July 23rd, 2005, 04:52 AM
Manfred
Guest
 
Posts: n/a
Default Re: How to use a filename string with fstream?

The method c_str() does this for you.
Example:
[color=blue]
>string sFileInfo = "MyFile.tmp";
>fstream InfoFile;
>InfoFile.open(sFileInfo, fstream::app);[/color]

InfoFile.open(sFileInfo.c_str(), fstream::app);
would be the replacement of the previous line.
[color=blue]
>// >> do some i/o operations <<
>InfoFile.close();
>return 0;[/color]

This should do it.

Manfred
  #5  
Old July 23rd, 2005, 04:53 AM
Peter Julian
Guest
 
Posts: n/a
Default Re: How to use a filename string with fstream?


"DellBoy" <professorcad@hotmail.com> wrote in message
news:1117281610.334518.250070@o13g2000cwo.googlegr oups.com...[color=blue]
> 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?
>[/color]

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;
}

  #6  
Old July 23rd, 2005, 04:53 AM
DellBoy
Guest
 
Posts: n/a
Default Re: How to use a filename string with fstream?

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.