Got to next line from current line position in a file

Hi,

I am just few months after starting to lean C++.

I wrote the below code & it worked to retrieve a string from a file.
Two variables to fine tune what to capture after finding the required starting & ending string

int lnstarts, int leadingln

My question is how to perform the below line with something like below?

int line1=line+1
str=line1.substr(pos1+lnstarts, pos2-pos1-lnstarts);

I know line is a string type but just for the question I am using it to ask how to go to next line to capture.

The string in current line & next line is almost same.

Is there a way to go to next line from current line position?

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string Getstring (string FileName, string start, string Send, int lnstarts, int leadingln);

int main()
{
string filename;
string start;
string Send;
int lnstarts;
int leadingln;

cout << "Enter filename - "<<endl;
getline (cin, filename);

cout << "Enter start of string - "<<endl;
getline (cin, start);

cout << "Enter end of string - "<<endl;
getline (cin, Send);

cout << "Enter length of start string - "<<endl;
cin >> lnstarts;

cout << "Enter length of leading end string - "<<endl;
cin >> leadingln;


cout <<"String got is -"<<Getstring (filename, start, Send, lnstarts, leadingln)<<"-"<<endl;

return 0;
}


string Getstring (string FileName, string start, string Send, int lnstarts, int leadingln)
{
string strName;
string line;
string str;

ifstream fileToSearch;
fileToSearch.open (FileName.c_str());
if(fileToSearch)
{
while(!fileToSearch.eof())
{
getline(fileToSearch, line);

unsigned int pos1=0, pos2=0, pos3=0;

while((pos1=line.find(start, pos1)) != string::npos)
{
pos3=line.find(Send, pos1);
pos2=pos3-leadingln;
str=line.substr(pos1+lnstarts, pos2-pos1-lnstarts);
pos1++;
}
}
}
fileToSearch.close();
return(str);
}

Thank you for your reply.

Mathew
halla
to capture every thing in a line ,yo need to include '\n' when capturing tha string
see what i mean

1
2
3
4
5
6
7
8
9
cout<<"entre film name :";
getline(cin,filmname,'\n');

cout<<"enter  your lovely book :";
getline(cin,book,'\n');

cout<<" your film name is "<<filmname<<endl;

cout<<"your lovely book is "<<book<<endl;





try to show that each time you capture information indipendly
Topic archived. No new replies allowed.