Jump to content
  • Advertisement

Archived

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

peter86

Streams in C++

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
use cin::getline(string,size):

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

  

#include <iostream>

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

Share this post


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

    
#include <iostream>

#include <string>

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 <fstream>

void main()
{
ifstream f;
string str;

f.open( "myFile");

getline( f, str);

f.close();
}


Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!