• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Beginners question

SsupernovaE

Golden Member
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?
 
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.
 
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.
 
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.
 
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.
 
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().
 
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).
 
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) )
.....
 
^^ 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.

 
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.
 
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?



 
Back
Top