Two functions working with getline on same fstream object c++ -


I have the following problem: I have these two functions for the first time reading a contact information from a TXT file, Symbols separated by the symbol '#' -

txt file # 1 Name: AAA Phone: 08782634 Phone: 0245637 Date: 23.34 Phone: 324324324 # 2 Name: BBB Phone: 99 999 < / Code>

and it finds the length of each contact (the number of numbers between each '#'). The second call first calls one and the contact should be printed, but it prints the second contact, not the first.

Is it possible that gateline somehow changes the stream from the first function, because the second function works when I first use a const int (hardcord capability)?

  int Contact :: FindNumberOfFields (std :: ifstream & in) {char * buffer = new char [1024]; Int cnt = 0; Int i = 0; Int pos = 0; While (in) {in.getline (buffer, 1024); If (strayer (buffer, '#')) {while (inline (buffer, 1024)) {if (! Strchr (buffer, '#') and strillan (buffer)) {cnt ++; } And {return cnt; }}}} In.clear (); In.close (); Remove buffer; } Zero Contact :: ReadContactFromStream (std :: ifstream & in) {SetCapacity (FindNumberOfFields (in)); // cout & lt; & Lt; GetCapacity () & lt; & Lt; Endl; // Output five is correct, while (in) {ifFields> = GetCapacity ()) // Added when corrected; Fields & gt; = Hardcored int (5) and SetCapacity (in) code from {break; } Contact type [Added field] = Create objectframeline (in); AddedFields ++; }}  

All the files have "current reading status", and as you The read file is advanced in that case unless you always read the next thing in the file, which is currently the next record for "# 2" .

My suggestion to solve this for you is just a function that reads the data, and when it comes to a new record marker it starts the empty string, and this vector Reads the lines of records in, and then passes this vector to the function that parses content

Something like the following pseudo code:

  std :: getline (stream, line); For (;;) {if (the line is the beginning of the record) {std :: vector & lt; Std :: string & gt; current record; While (std :: getline (stream, line)) {break (if the record is the starting line); // Internal loop and existing_record.but break out of push_back (line); } Parse_record (current_rcord); }}  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -