Skipping vector position when push_back is pressed

I am reading data from a file into a row vector data. And to this data vector I push_back a new line through my main called output_string. Output_string is just a combination of arguments passed through the command line. After doing all this, I will write back to my file (update the file with a new line). However, when I do this, everything after the command line argument 1, it skips the vector position whenever it encounters again data.push_back(output_string);.

e.g. file contents

bob
jack
snack

after reading into a vector,

vector data content

bob
jack
snack

after adding a new line, the new line, which is the content of the john data vector, becomes

bob
jack
snack
john

- ,

bob
jack
snack
john

peter

, . ?

int main (int argc, char *argv[]){


 if (argc > 6){
  cout<<"[Error] too many inputs provided" << endl;
  return 0;
 }

 commandProcess(argc,argv);

 outputstringformat();
 //*********
 if (cominput.rem_contpos == -1){
  readData();                        //reads data from a file into vector data
  int outlen = output_string.length();
  if (outlen > 0){
   data.push_back(output_string);  //pushing what i had in argv to vector
  }

  cout<<"----------data vector------------"<<endl;
  for (int i = 0; i < data.size();i++){
   cout<<"data: " << data[i] << endl;
  }

  ofstream outfile("contactlist.dat");
  number_of_contacts = data.size();
  if(outfile.is_open()){
   for (int i =0; i < number_of_contacts; i++){
    outfile << data[i] << endl; //copying evertthing back to file, including the new argument passed to data
   }
   outfile.close();
  }
 }
 return 0;
}

EDIT: , , . , , ...: |

void outputstringformat(){

    if (cominput.name1.length() != 0 ){
        output_string = cominput.name1;
    }   
    if (cominput.name2.length() != 0 ){
        output_string = output_string + " " + cominput.name2;
    }
    if (cominput.name3.length() != 0 ){
        output_string = output_string + " " + cominput.name3;
    }
    if (cominput.email.length() != 0 ){
        output_string = output_string + " " + cominput.email;
    }
    if (cominput.phone.length() != 0 ){
        output_string = output_string + " " + cominput.phone;
    }
}

reaData​​strong >

void readData(){
    ifstream myfile("contactlist.dat");
    if(myfile.is_open()){
        while(!myfile.eof()){
            getline(myfile,line);
            data.push_back(line);
        }
        myfile.close();
    }
}
+3
3

, , :

"bob\n"
"jack\n"
"snack" // no line feed

"bob", "jack" "snack". "john" endl, :

"bob\n"
"jack\n"
"snack\n"
"john\n" // line feed

, "bob", "jack", "snack", "john" ", , " john\n" , .

EDIT: readData, , :

void readData() {
    ifstream myfile("contactlist.dat");
    if(myfile.is_open()){
        string l;
        while(myfile >> l){
            data.push_back(l);
        }
        myfile.close();
    }
}

http://codepad.org/INgm757m, , , contactlist.dat .

+1

:

while(!myfile.eof()){
    getline(myfile,line);
    data.push_back(line);
}

myfile.eof() , ; , EOF. , getline (- EOF), line .

:

while(getline(myfile,line)) {
    data.push_back(line);
}    

, getline - , (myfile ), iostreams , , if (myfile), , , EOF. , EOF , getline myfile, false while, .

, EOF.

+2

There are not many here, but I suspect that it is readData()adding an empty string to the vector. Try the following:

// Put this right after readData()
cout<<"----------data vector from file------------"<<endl;
for (int i = 0; i < data.size();i++){
  cout<<"data[" << i << "]: " << data[i] << endl;
}

EDIT:
I am not familiar with your version getline, but it looks like it is collecting the final CR or LF or something else. Try the following:

void readData(){
    ifstream myfile("contactlist.dat");
    if(myfile.is_open()){
        while(!myfile.eof()){
            getline(myfile,line);
            if(myfile.gcount()>0){  // checking for zero-length string
                data.push_back(line);
            }
        }
        myfile.close();
    }
}
0
source

Source: https://habr.com/ru/post/1761024/


All Articles