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();
int outlen = output_string.length();
if (outlen > 0){
data.push_back(output_string);
}
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;
}
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;
}
}
reaDatastrong >
void readData(){
ifstream myfile("contactlist.dat");
if(myfile.is_open()){
while(!myfile.eof()){
getline(myfile,line);
data.push_back(line);
}
myfile.close();
}
}