Question:
ifstream c++ HELP!!!!?
George
2011-09-15 12:56:16 UTC
Hello, i have a simple question about reading data from .txt file.
for example i have ifstream variable READ.
then i open READ (file.txt)
then we have to copy data into string right???
so when we do that we write READ >> mystring
my question is how does READ >> mystring work? i searched everywhere but i couldnt find it, even in book author didn't explain how the hell it works. in tutorials they just say how to do but nobody explains what exactly it does.

Please hhhheeeeeellllllllllppppppppppppp
Five answers:
Redsatori
2011-09-15 13:09:02 UTC
There are two stream operators in C++: operator>> and operator<<. These operators, when applied to stream objects (fstreams, istreams, ostreams, etc) allow data to pass between them.



The operator>> is generally known as the extraction operator, in that it extracts data from the stream and inserts it into the object. In your example, extracting data from the READ object and inserting it into the mystring object.



The operator<< is generally known as the insertion operator. It does the opposite of the extraction operator in that it inserts data into the stream object from an outside object. For instance, if you had a WRITE output stream, you could do WRITE << mystring which would put mystring's value into the WRITE stream.



Good luck!
jplatt39
2011-09-15 13:25:59 UTC
Redsatori's answer is excellent of course. I'm sure he would have a much higher percentage of answers than I do (which he does) even if I didn't answer such diverse topics as this, mythology and folklore and drawing and illustration. (so of course my percentage is lower). I still think you might understand better if I take a different, very roundabout approach. Bear with me.



Where do you use the >> and << operators? With cin and cout. Are they commands? No. Most of the documentation I see refer to them as objects which correspond to the keyboard -- or stdin in C and the screen -- stdout in C. Now C evolved with UNIX, which was written in it originally as they were evolving C from B. And one particular aspect of UNIX is that it treats everything as a file. Everything. This is something that C shares with it and it is something C++ inherited, though we used to just call them streams before strings were introduced to complicate things. The keyboard and screen are NOT files, but you can TREAT them as such in C/C++, and this is where the operators come in. When you write READ >> mystring; the compiler makes the program look in the file buffer for anything which looks like a string -- that is letters followed by either EOF or a whitespace character, and copies the result into mystring. Similarly when you write to an ofstream called WRITE you could say WRITE << "I drank " << numbottles << " of Root Beer." << endl; and it would write a string terminated by a newline if it is a text file.



Clear?
Cubbi
2011-09-15 13:31:08 UTC
There really isn't much need to know *how* it works, for all practical purposes, it reads a whitespace-delimited string from file. But sure, here are some additional details:



the expresion "READ >> mystring" where READ is of type std::ifstream and mystring is of type std::string is parsed. The compiler examines namespace std (since both arguments belong to that namespace) and finds many overloads of operator>>() each handling a different argument type. Among all those overloads, the best match is this:



template basic_istream& operator>>(basic_istream& is, basic_string& str);



Now, when this operator>> is called, the compiler does the following (I'm omitting the sentry object and some error handling)

1) calls mystring.erase(), which deletes all characters currently present in the string

2) obtains a reference to the locale used by the istream

3) obtains a reference to the character classification facet (ctype) from that locale

4) extracts characters from the filebuf subobject of READ one by one, (as if by calling snextc()), until it finds a character not classified as whitespace by the facet (unless you've done READ >> noskipws earlier)

5) extracts characters from the filebuf subobject of READ one by one until it finds a character classified as whitespace by the facet or filebuf indicates end of file. For each character c that was extracted, it calls mystring.append(1, c)

6) it returns a reference to READ in case you wish to chain the input operators or test for possible errors.
?
2011-09-15 15:15:50 UTC
So many good explanations here. However I don't share your assumption that after opening a text file "we have to copy data into string", where it is my understanding that you are referring to a basic_string object. What ">>" does is usually determined by the format of the text file, which usually is reflected in a class.



You could have a text file with the i.e. following repeating pattern:

Sequence Number

Item Description

Item Abbreviation

Item Price



and choose to read this into the following structure:



class CItem {

private:

int iSeqNO;

string strDesc;

char szAbbrev[5];

float flPrice;

};



In this case the parsing process would not only perform the character by character read including whites space detection, but additionally would perform the necessary type conversions as well. Programmatically this case would typically be covered in the operator>> overload as follows:



READ >> iSeqNo >> strDesc >> szAbbrev >> flPrice;



I hope this is helpful.
Chris C
2011-09-15 13:24:46 UTC
The stream operator ">>" reads information from a text file.

If you do "READ >> mystring;", that will read every character up until a whitespace character is found (tab, space, EOL).

So READ >> mystring;

  on: "Testing" would assign "Testing" into mystring

  on: "Testing now" would assign "Testing" into mystring

  on: "Testingnow" would assign "Testingnow" into mystring



If you want to read more than one word, you'll have to either call "getline(READ, mystring);", or read words in until some other non-whitespace delimiter is found, and append it to 'mystring'.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...