0

my .txt file contains information like

abc,bcd,10,20.  
cde,efg,30.  
hello,there,40

how can i read the last value, in this case 20,30 and 40 and it is between , and .

ifstream myfile;
    myfile.open("myfile.txt");
    string doctor;
    for(int j=1;j<=5;j++)
    {

        for (int i = 1; i<=1; i++)
        { 
            getline(myfile,doctor,'.');

        }

        cout<<doctor<<endl;

        getline(myfile,doctor);
    }
    myfile.close();

here, i am getting the whole line

aatanka
  • 31
  • 3
  • string _20 = myfile[myfile.size()-3] + myfile[myfile.size()-2]; – Omid CompSCI Aug 23 '17 at 15:20
  • Note that is only if you know exactly where the value is, but it seems like you are looking more towards a search functionality. Look into string functions. Look at: http://www.cplusplus.com/reference/string/string/ – Omid CompSCI Aug 23 '17 at 15:21
  • Why do you use a `for` loop for only 1 iteration? Seems like a waste; get rid of the loop or correct it. – Thomas Matthews Aug 23 '17 at 16:01
  • In many programs, the entire record is read from the file into memory, then the record is examined. If the record does not match the search criteria, another record is read. A more efficient method is to read many records into memory since searching memory is faster than reading from the file. – Thomas Matthews Aug 23 '17 at 16:02
  • Actually i have multiple lines of same kind @ThomasMatthews – aatanka Aug 24 '17 at 12:26

0 Answers0