• Popular Now

• 9
• 9
• 11
• 12
• 9

Archived

This topic is now archived and is closed to further replies.

Streams in C++

This topic is 5898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

How do I get the whole line when I input something from a file to a variable?

Share on other sites
use cin::getline(string,size):

where string is the string you read into, and size is the size of the string

  #include main(){ char somestring[256]; cout << "type a string" << endl; cin.getline(somestring,sizeof(somestring)); cout << "you typed: " << somestring << endl;}

Share on other sites
No, use getline(basic_istream&, basic_string&), declared in string.

  #include #include int main(void){ using namespace std; string str; cout << "Type something and then press return: "; getline(cin, str); cout << "You typed " << str << endl;}

Edited by - DrPizza on February 24, 2002 6:37:06 AM

Share on other sites
(Someone has got to tell me how to do those cool code block things above)

I think he asked about getting input from a file? In that case, you can try the following:

------------------------
if( (fFile = fopen( "Data.txt", "r" )) == NULL )
exit (EXIT_FAILURE);

fread( pData, sizeof( char ), nCharCount, fMapFile );

fclose( fMapFile );
------------------------

But, for the above you need to know nCharCount (the amount of characters(bytes) in the file). You could also use:

------------------------
if( (fFile = _open( "Data.txt", _O_RDONLY )) == -1 )
exit(EXIT_FAILURE);
bytesread = _read( fh, buffer, nbytes ) );
------------------------

The above code will run and read through the file until it hits a CTRL_Z character, which signifies EOF (end of file.)

We know nothing

Share on other sites
I''m sure he did -- but one istream is much like any other istream, so it doesn''t matter a hoot.

Share on other sites
YOu type "source" and "/source" in square brackets to get the source thingy.

Anyway, I was wondering if anyone knew how youcoul code your own stream functions... Rather than just for input/export?

Is it possible and what kind of things would see them applied?

Oli

All the best problems start with C

.:: K o s m o s E n g i n e ::.

Share on other sites
What i want to know is how i input a whole line from a file to a variable, not to let the user input something.

Share on other sites
quote:
Original post by peter86
What i want to know is how i input a whole line from a file to a variable, not to let the user input something.

Just do the same thing with a file stream instead of cin.

  #include void main(){ ifstream f; string str; f.open( "myFile"); getline( f, str); f.close(); }

Share on other sites
It dosen´t work =(!

Share on other sites
What does "it doesn''t work" mean? What did you try? Did it fail to compile, fail to run, do something unexpected, crash? As a point of interest, I''ve not seen any mention in this thread of which language you are using. Since this forum is about "General Programming", it could be any language. Most people seem to have assumed you are using C++, but is that the case? Ask questions the smart way, and state exactly what you are trying to do and, if something goes wrong, state what the symptoms are.

--

The placement of a donkey''s eyes in its head enables it to see all four feet at all times.