c++ newbie question

C

Cheryl

how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
 
C

Chris Theis

Cheryl said:
how can i read a string from a text file in c++ using fstream.h?
e.g.
in the text file
Why do you want to read it as a char array? You're far better of reading it
as a string because you don't need to know in advance how many characters
you're going to read. Afterwards you can still assign it to a char array if
you want/need to.

string MyString;
FileStream >> MyString;

In case you want to read a whole line of words that are delimited by spaces
you might consider using the getline function. Anyway, look up streams in
any decent C++ beginner's book.

Chris
 
R

Rolf Magnus

Cheryl said:
how can i read a string from a text file in c++ using fstream.h?

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
of the non-standard said:
e.g.
in the text file

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

Karl Heinz Buchegger

osmium said:
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 :)

Unless, of course, the assignment asks for a solution using a character array.
 
O

osmium

Chris said:
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.

Cheryl: std::istream::get() will read a character at a time. Keep looking
for a terminating '\0' and buffer overflows. This will be quite inefficient
due to large numbers of functions calls, but it should be good enough for
your current problem.
 
C

Chris Theis

osmium said:
Perhaps she didn't write the file? It seems quite plausible that the
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... :)

Chri
 
H

Howard

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?

I think you are not grasping the concept of reading into the string class
via a stream. Doing it this way allows you to read data in just as it would
when reading into a char array, but the string class provides better
control, ease of use, and more functionality. It's not that you're loading
a persisted structure from the file, it's that you're reading in characters
(or lines of characters) into a string variable, which handles the dirty
work for you. It's much easier and cleaner!

-Howard
 
J

Jay O'Connor

Chris 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.

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... :)

If the instructor created the file, that means it's an assignment of
some sort, in which case their may be requirements in the assignment to
use a char array rather than a string, depending on what is being taught.

Take care,
Jay
 
O

osmium

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 :)

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?
 
C

Cheryl

problem solved
thx
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;
}
 
C

Chris Theis

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.

Whoa, relax pal! We're talking about the simple problem to read a string or
text from a file. This is not about persistency or any other sophisticated
concept. Furthermore even if you assume that an unknowable "blob" is stored
on the medium you'll first have to get some information what that blob is,
trigger for example a generic factory and tell the spawned object to read
itself in. Thus, without any knowledge nothing is possible. The statement
that using a string class or "knowing" that the mou x-y coordinates are
always at location such and such is way out of line. Using the string class
you do not have to know anything about its internal representation, the only
thing you have to know is that you expect a string. The solution reading a
text character by character is extremely tedious and error prone and IMHO
should only be resorted to, if there are good reasons for that.
Or are you suggesting that the instructor goofed?

No, Karl Heinz did not suggest that the instructor goofed and neither did I.
Though there are some instructors that should certainly catch up on their
C++ knowledge (which is just a general comment and opinion of myself!)

Regards
Chris
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top