• Advertisement

Archived

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

Streams in C++

This topic is 5811 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
I''m sure he did -- but one istream is much like any other istream, so it doesn''t matter a hoot.

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
It´s in C++ and it dosen´t work when i run it, it dosen´t crash or something, but i don´t get the output that I want.

Share this post


Link to post
Share on other sites
Show us the code! And tell us what you expect it to do.

--

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
quote:
Original post by SabreMan
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?


The title "Streams in C++" pretty much gave it away for me

Share this post


Link to post
Share on other sites
Here is the code:

#include <fstream.h>

void main
{
ifstream fin("file.txt");
char string;
fin.getline(&string,sizeof(string));
cout << string;
}

I except it to print the whole first line in file.txt.

Share this post


Link to post
Share on other sites
Several problems:

- you should not be using the old-style headers.
- don't use char array, use std::string.
- use the global getline() function to get a line into a string.

Here's a modified version of the code:

    
#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main()
{
ifstream fin("file.txt");
string s;
getline(fin, s);
cout << s;
}


Oh, and the signature of main() must always return an int. Returning void is illegal. If you don't explicitly return an int, the compiler inserts a "return 0" into your code.

--

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


Edited by - SabreMan on February 24, 2002 10:58:26 AM

Share this post


Link to post
Share on other sites
  

#include <stdio.h>
#define LENGTH 128

FILE f;
char string[LENGTH];

if(f = fopen("file.txt", "r") == NULL)
return;
else
{
fgets(string, LENGTH, f);
fclose(f);
}



btw: char *fgets(char *s, int n, FILE *stream);
hope this helps

Share this post


Link to post
Share on other sites
If you''re using C++, please use C++. SabreMan''s preceding example is the definitve C++ solution (a modification of DrPizza''s to handle fstreams). std::getline handles all istream-derived objects (make sure to use the new, sans-''.h'' extension standard library header files).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Oh, and the signature of main() must always return an int. Returning void is illegal. If you don''t explicitly return an int, the compiler inserts a "return 0" into your code.



actually, its OK if you dont return a number in main()

Share this post


Link to post
Share on other sites
quote:
Original post by barazor
actually, its OK if you dont return a number in main()


As long as you declare it void (I think ANSI standard says that you''re supposed to return a value, but every compiler I''ve ever seen lets you declare main() as a void)



"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

Share this post


Link to post
Share on other sites
quote:
Original post by matrix2113
As long as you declare it void (I think ANSI standard says that you''re supposed to return a value, but every compiler I''ve ever seen lets you declare main() as a void )



Rather than second guessing what the Standard says, why don''t you read what it says? It states that not returning a value from main() is an implicit return of zero. It also states that any signature of main involving anything other than an int return type is illegal. MS may get that wrong, but they don''t define the Standard.

Share this post


Link to post
Share on other sites

  • Advertisement