Help with fstream

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: ghoststriker7 is an unknown quantity at this point 
Solved Threads: 0
ghoststriker7 ghoststriker7 is offline Offline
Newbie Poster

Help with fstream

 
0
  #1
Oct 21st, 2004
okay i know how to use fstream and everything, but the prolem is that I want to use a variable in ithe open part.

exp const.

pretend all headers are included.

It works like this
{
...

ifstream infile;

infile.open("text.txt");
...
}

but when I want to use a variable for the file it will read it does not work/


exp. using variable
{
...

ifstream infile;

string text = "text.txt";

infile.open(text);

...
}
it does not work, any helpful hit please.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 185
Reputation: Stack Overflow is an unknown quantity at this point 
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: Help with fstream

 
0
  #2
Oct 21st, 2004
Greetings,

The reason you are experiencing this problem is because fstream's open() command takes a "char *" paramater, not string.

open()
void open(const char *filename, openmode mode = in | out);
> Opens a file. The stream's file buffer is associated with the specified file to perform the i/o operations.

basic_string
The basic_string class represents a Sequence of characters. It contains all the usual operations of a Sequence, and, additionally, it contains standard string operations such as search and concatenation.

The basic_string class is parameterized by character type, and by that type's Character Traits. Most of the time, however, there is no need to use the basic_string template directly. Note also that, according to the C++ standard, basic_string has very unusual iterator invalidation semantics.

Converting
It is possible to convert a string to a character array. For example:
char str[255];
string s = " .... ";
strcpy( str, s.c_str() );
This is quite simple to understand.

const charT* c_str() const
> Returns a pointer to a null-terminated array of characters representing the string's contents.

The same with sending fstream the const char data. Instead of sending the string, send string.c_str(). For example:
  1. ifstream infile;
  2.  
  3. string text = "text.txt";
  4.  
  5. infile.open(text.c_str());
If you have further questions, please feel free to ask.

- Stack Overflow
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: ghoststriker7 is an unknown quantity at this point 
Solved Threads: 0
ghoststriker7 ghoststriker7 is offline Offline
Newbie Poster

Re: Help with fstream

 
0
  #3
Oct 21st, 2004
I didn't even know that command existed, or that fstream only takes chars, thanks alot. :cheesy:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC