+ 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
16 Réponses
+ 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() ;
+ 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);
???
+ 3
Where are you recieving input in your code?
I cant find that.
Have you tried using getline()?
+ 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?
+ 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;
}
+ 3
please help me out here
+ 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;
+ 3
Send me the entire code...
Ill have to check every part...
Whats the problem now?
Same as before?
Or something new?
+ 2
@kinshuk this is just a function, original code is too big so I can't post it. I call output like:
GotoLine();
+ 2
@kinshuk i do something like:
cout<<"xyz is:"<<endl;
GotoLine(file,2);
+ 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;
+ 2
@anonymous
Happy to help... 😊
+ 1
nope ;_; what should i do now?
+ 1
@kinshuk here's the entire code
+ 1
@kinshuk holy shitttt it's working 😭 thanks alot bro really appreciate that!