Lets say i have a file with 20 lines...
How do i read just line 14 or any other line i want alone with out reading the rest?
Lets say i have a file with 20 lines...
How do i read just line 14 or any other line i want alone with out reading the rest?
You read one line at a time. Use a counter if you want a specific number of lines to read.
Your question seems quite vague because the answer is too simple. How are you reading from the file?
if you know the length you can use
http://www.cplusplus.com/ref/iostrea...eam/seekg.html
i would jsut read in one line at a time, possibly store each string
into a vector just incase im going to end up using all the
lines, and simply call the spot in the vector for the line i wanted
ex
then again im kind of parcial to vectors, hense the name :/Code:vector<string> vS; string S; ifstream file("myfile.txt"); while(!file.fail()) { getline(file, S, '\n'); vS.push_back(S); } file.close(); cout << vS[13] << endl; //for line 14 of the file
of course these will require :
Code:#include <iostream> #include <fstream> #include <string> #include <vector> using namespace std;
Last edited by ILoveVectors; 06-24-2005 at 10:41 AM.
Your example code will add an extra entry to the vector you don't want. Use while (getline(file, S, '\n')) instead.
lets say i have teh number 123 stored in a string is there a way to put into int?
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
If you mean actual string and not character array,
if you do mean character array look in the faq, its there
somewhere to.