| | |
how to move to the second line in C++ .txt file reading?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Reading from a file works the same way as reading from any other istream, such as cin:
With files, replace cin with the name of your ifstream
C++ Syntax (Toggle Plain Text)
//read up to newline std::string str; std::getline(cin, str);
C++ Syntax (Toggle Plain Text)
//read up to whitespace (after a word or character) std::string str; cin >> str;
Last edited by Bench; Nov 4th, 2006 at 7:31 am.
¿umop apisdn upside down? •
•
•
•
Reading from a file works the same way as reading from any other istream, such as cin:
C++ Syntax (Toggle Plain Text)
//read up to newline std::string str; std::getline(cin, str);With files, replace cin with the name of your ifstreamC++ Syntax (Toggle Plain Text)
//read up to whitespace (after a word or character) std::string str; cin >> str;
and just repeat that three times.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Just repeat the code Bench posted until you get to the desired line. You will have to use a loop and a counter integer to keep track of the line number that was just read. If you are confused about loops -- which is understandable -- you should read about them in your text book.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
•
•
but how to use pointer to move the reading postion? point to what?
The chunk of data you read is always at the "head" position - The tape moves forward, rather than the head (and since you can't rewind, the data which has already passed the head is gone from the stream). You can choose to either store the data at the head or discard it. The tape rolls on once the head has read whatever's at that position. Whatever you do, you *must* read it in order to move the tape on.
here's a quick example of reading the 3rd word in a stringstream (Which is just another 'flavour' of streams - fstreams work in exactly the same way)
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> int main() { std::stringstream ss("the quick brown fox jumped over the lazy dog"); std::string str; const int position(3); for(int i(0); i!=position; ++i) ss >> str; std::cout << str; }
Last edited by Bench; Nov 4th, 2006 at 10:51 am.
¿umop apisdn upside down? ![]() |
Other Threads in the C++ Forum
- Previous Thread: Mersenne Primes Help!!
- Next Thread: type convert problem 2
Views: 8887 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






