+ 1

[c++] How to Read text from a specific line in c++

hey y'all. I'm trying to read data from a txt file which has data like: > chandler M bing data analyst nyc 22-22-22 central perk, Brooklyn. < when I use the code which I've mentioned, it only reads data like: > chandler data nyc 22-22-22 central < what can I do here? please and thanks for your time. https://code.sololearn.com/cgABfKJ15vNa/?ref=app

6th Jun 2017, 7:00 AM
anonymous
16 Respostas
+ 3
https://code.sololearn.com/cHm2F5888Iwn/?ref=app Heres the code, corrected. Just did a few changes... And yes, change the paths of the file to serialname only... Remove the "/storage/emulated/...." + part and rename that to serialname.c_str() ;
6th Jun 2017, 10:14 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 4
And then if suppose the file object is : fin... Do you read the data like this?: string a; GotoLine(fin,2); fin>>a; or string a; GotoLine(fin,2); getline(fin,a); ???
6th Jun 2017, 7:24 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Where are you recieving input in your code? I cant find that. Have you tried using getline()?
6th Jun 2017, 7:15 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
@anonymous But, GotoLine doesn't have anything to read! It just ignores the rest of the data till the point. Then what does it use to read?
6th Jun 2017, 7:51 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Maybe its the return file part... I think you should rephrase the function to : string GotoLine(std::fstream& file, unsigned int num) { string st; file.seekg(std::ios::beg); for(int i=0; i < num - 1; ++i) { file.ignore(std::numeric_limits <std::streamsize>::max(),'\n'); } getline(file,st); return st; }
6th Jun 2017, 8:02 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
please help me out here
6th Jun 2017, 8:21 AM
anonymous
+ 3
@anonymous Try using my function like this: cout<<"xyz is - "<<GotoLine(file,0); Or your own function like this: string a; GotoLine(file,0); cout<<"xyz is - "; getline(file,a); cout<<a;
6th Jun 2017, 8:28 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Send me the entire code... Ill have to check every part... Whats the problem now? Same as before? Or something new?
6th Jun 2017, 8:45 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
@kinshuk this is just a function, original code is too big so I can't post it. I call output like: GotoLine();
6th Jun 2017, 7:22 AM
anonymous
+ 2
@kinshuk i do something like: cout<<"xyz is:"<<endl; GotoLine(file,2);
6th Jun 2017, 7:36 AM
anonymous
+ 2
@kinshuk no mate still same result. i copy pasted the function as it is and called it in main like: gotoline(file,2); cout<<"xyz is...... "<<endl; string line2; file>>line2; cout<<line2; cout <<endl;
6th Jun 2017, 8:21 AM
anonymous
+ 2
@anonymous Happy to help... šŸ˜Š
6th Jun 2017, 10:40 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
nope ;_; what should i do now?
6th Jun 2017, 8:41 AM
anonymous
6th Jun 2017, 9:20 AM
anonymous
+ 1
@kinshuk here's the entire code
6th Jun 2017, 9:20 AM
anonymous
+ 1
@kinshuk holy shitttt it's working šŸ˜­ thanks alot bro really appreciate that!
6th Jun 2017, 10:38 AM
anonymous