C
Cheryl
how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
e.g.
in the text file
Why do you want to read it as a char array? You're far better of reading itCheryl said:how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
Cheryl said:how can i read a string from a text file in c++ using fstream.h?
of the non-standard said:e.g.
in the text file
osmium said:Perhaps she didn't write the file? It seems quite plausible that the
instructor provided a file.
Chris said:Why do you want to read it as a char array?
osmium said:Perhaps she didn't write the file? It seems quite plausible that the
instructor provided a file.
It seems to me that would violate a cardinal rule of object oriented
programming. There should be no way for the user of a class to determine
the methods of representation of that class, either in RAM or on mass
storage. It should be assumed that there is an unknowable "blob" on the
storage medium. I don't know how strings are stored, I don't want to know
and I shouldn't know - no one should tell me. I see no difference between
this proposal and "knowing" that the mouse x-y coordinates are always at
location such and such.
Or are you suggesting that the instructor goofed?
Chris said:Chris Theis writes:
Perhaps she didn't write the file? It seems quite plausible that thehow can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
--------------
abcdefg
--------------
how can i read that as char* or char[]?
thx
Why do you want to read it as a char array?
instructor provided a file.
Sorry, I may be a little thick today but what does it matter who created the
file? AFAIK the choice of string or char array is independent of the creator
of the file, but probably I might be mistaken here...
Karl said:osmium said:Chris said:how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
--------------
abcdefg
--------------
how can i read that as char* or char[]?
thx
Why do you want to read it as a char array?
Perhaps she didn't write the file? It seems quite plausible that the
instructor provided a file.
Even then std::string would be a better choice
Rolf Magnus said:What do you want to read into the string? A line? Part of a line? The
whole file?
Anyway, I'd recomment using the standard streams from <fstream> instead
And here, I'd recomment using std::string instead of char arrays.
#include <fstream>
#include <string>
#include <iostream>
int main()
{
using namespace std;
ifstream file("file.txt");
string line;
while (getline(file, line))
cout << line << endl;
}
osmium said:Karl said:osmium said:Chris Theis writes:
how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
--------------
abcdefg
--------------
how can i read that as char* or char[]?
thx
Why do you want to read it as a char array?
Perhaps she didn't write the file? It seems quite plausible that the
instructor provided a file.
Even then std::string would be a better choice
It seems to me that would violate a cardinal rule of object oriented
programming. There should be no way for the user of a class to determine
the methods of representation of that class, either in RAM or on mass
storage. It should be assumed that there is an unknowable "blob" on the
storage medium. I don't know how strings are stored, I don't want to know
and I shouldn't know - no one should tell me. I see no difference between
this proposal and "knowing" that the mouse x-y coordinates are always at
location such and such.
Or are you suggesting that the instructor goofed?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.