Beginners question

SsupernovaE

Golden Member
Dec 12, 2006
1,128
0
76
If you have .txt file that contains something like the following:

Hello, my name is Bob and my e-mail is Bob@Bob.com.

Can you use the extraction operator >> to extract words from the file and store them into variables of type string?
 

cKGunslinger

Lifer
Nov 29, 1999
16,408
57
91
Pretty much, yes. This may the "old" C way of doing it, but look into fopen(), fclose(), etc. It's been a while since I used C++, but I'm sure there's some way to open a file as a stream.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: cKGunslinger
Pretty much, yes. This may the "old" C way of doing it, but look into fopen(), fclose(), etc. It's been a while since I used C++, but I'm sure there's some way to open a file as a stream.

I generally prefer FILE*'s (e.g. fopen()) to istream's, as it makes it clearer where the system calls are and gives me more control over buffers. I also never bothered to learn the C++ magic (if it exists) to make file descriptors non-blocking, non-buffered, etc., so I just use C-style when I do stuff like that.
 

polarbear6

Golden Member
Jul 14, 2008
1,161
1
0
no matter what u try if ur using vista first get permissions for ur own files first
*yuck* vista
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Among the links Mark posted this seems to be the closest we get to what you're asking,

http://www.java2s.com/Tutorial...loatvaluefromafile.htm

If you look at that, we see that you CAN use >> to read from a file, but it will do so up to the next whitespace.

Can I ask what is the requirement to use >> in particular ? Is it in a discussion you had with some friends?
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
You could redirect the stdin stream with the operating system... For example, if your program is called "MyProgram" and your text file is called "info.txt", you could call your program like so:

MyProgram < info.txt

^^ then you'd use "cin" like you normally would on the command line. I don't recommend this at all.


The best way to do this, honestly, is how degibson described.
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
As Markbnj stated, in C++ the easiest way to do what you want is to use ifstream.

Here is an example: http://downloads.moonlapse.org/main.cpp

You can use ifstream and the >> operator to get every chunk of text between white spaces.

For example purposes I stored the words in a doubly linked list and printed its contents at the end.

I assume this is the sort of thing you were going for.
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
Originally posted by: Dimmu
As Markbnj stated, in C++ the easiest way to do what you want is to use ifstream.

Here is an example: http://downloads.moonlapse.org/main.cpp

You can use ifstream and the >> operator to get every chunk of text between white spaces.

For example purposes I stored the words in a doubly linked list and printed its contents at the end.

I assume this is the sort of thing you were going for.

Also keep in mind that since ifstream inherits from istream just like cin, you can use many of the functions used in cin, like getline().
 
Sep 29, 2004
18,656
68
91
I forget the function, but there is one where you provide a string and a delimiter and it will return an array of strings (pointers).
 

Fullmetal Chocobo

Moderator<br>Distributed Computing
Moderator
May 13, 2003
13,704
7
81
Originally posted by: IHateMyJob2004
I forget the function, but there is one where you provide a string and a delimiter and it will return an array of strings (pointers).

getline(), correct? getline will get the entire string, including the spaces, while cin will get up to the next space (or end of line).

I've seen it used like this:
ifstream fin;
fin.open(filename.txt);
char line[80];
while ( fin.getline(line,80) )
.....
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
^^ then you'd use "cin" like you normally would on the command line. I don't recommend this at all.

Why? Being able to pipe output from one program into the stdin of another is one of the reasons why cli unix tools are so awesome.

 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Nothinman
^^ then you'd use "cin" like you normally would on the command line. I don't recommend this at all.

Why? Being able to pipe output from one program into the stdin of another is one of the reasons why cli unix tools are so awesome.

I wouldn't recommend this strategy without some additional option to freopen stdin. Overloading cin/stdin with a chance to also define a command-line input file is awesome.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: manju
... Additionally, some of the features of C++ allow low-level access to memory but also contain high level features.

What exactly are you calling a 'low-level' access to memory and how does it differ from a 'high-level' access?