| | |
Help with fstream
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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: 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: If you have further questions, please feel free to ask.
- Stack Overflow
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() );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:
C++ Syntax (Toggle Plain Text)
ifstream infile; string text = "text.txt"; infile.open(text.c_str());
- 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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
![]() |
Similar Threads
- fstream Tutorial (C++)
- need help with fstream input files (C++)
- probelm with <fstream>; saving characters (C++)
- fstream and struct question (C++)
- Stack Queue Fstream (C++)
- basic fstream stuff (C++)
Other Threads in the C++ Forum
- Previous Thread: Amature Program- Lacking
- Next Thread: Nested loop that runs until user enters 'q'
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





