Trouble writing entire lines of string to file

Alright guys! Having an issue with writing to a file. Basically, my problem is that when I am writing to the file, for example an address such as 2913 Harvey Drive in the variable person.address1 would be written in the file on separate lines instead of one line. I need them on one line. I did some research and couldn't find anything that I understood so I was hoping that I could have an example as it relates to my code to help me understand. Here is my code:

int newEntry()
{
string str;
Client person;
char response;
double temp1;
double temp2;
ostringstream strs1;
ostringstream strs2;

//create file object and open file
fstream customer("customer.dat", ios:: out | ios::app);

if (!customer)
{
    cout << "Error opening file. Program aborting." << endl;
}

do
{
    cout << "Enter client information:" << endl << endl;
    cout << "Name:               ";
    cin  >> person.name;
    cout << endl;

    cout << "Address 1:          ";
    cin  >> person.address1;
    cout << endl;

    cout << "Address 2:          ";
    cin  >> person.address2;
    cout << endl;

    cout << "Phone:              ";
    cin  >> person.phone;
    cout << endl;

    cout << "Acct. Balance:      ";
    cin  >> temp1;
    strs1 << temp1;
    str = strs1.str();
    person.acctBal = str;
    cout << endl;

    cout << "Last Payment:       ";
    cin  >> temp2;
    strs2 << temp2;
    str = strs2.str();
    person.lastPay = str;
    cout << endl;

    customer << person.name << endl;
    customer << person.address1 << endl;
    customer << person.address2 << endl;
    customer << person.phone << endl;
    customer << person.acctBal << endl;
    customer << person.lastPay << endl;
    customer << endl;       //blank space in file

    cout << endl << "Do you want to enter another record? (Enter Y for Yes, N 
                             for No):   ";
    cin  >> response;
    //add validation to make sure they enter y or n
    cout << endl << endl;
    cout << BORDER << endl << endl;


} while (toupper(response) == 'Y');

customer.close();

return 1;
}

I saw something on concatenating a string variable then saving the ENTIRE string variable to the file, but I'm not sure if I would understand how to read each line back either. Here is the function that reads the code:

int displayAll()
{
vector<Client> store;
string space;
Client foo;
int i = 0;

fstream customer("customer.dat", ios::in);

if (!customer)
{
    cout << "Error opening file. Program aborting." << endl;
    return 0;
}

while (!customer.eof())
{
    store.push_back(foo);

    customer >> store[i].name;
    customer >> store[i].address1;
    customer >> store[i].address2;
    customer >> store[i].phone;
    customer >> store[i].acctBal;
    customer >> store[i].lastPay;
    customer >> space;

    i++;
}

for (int k = 0; k < i; k++)
{
    cout << "   Name:               " << store[k].name << endl
         << "   Address 1:          " << store[k].address1 << endl
         << "   Address 2:          " << store[k].address2 << endl
         << "   Phone:              " << store[k].phone << endl
         << "   Acct. Balance:      " << store[k].acctBal << endl
         << "   Last Payment:       " << store[k].lastPay << endl << endl;
}

cout << BORDER << endl << endl;

customer.close();
return 1;

}

So if I, for example, used a concatenating method such as:

string += person.name; string += newLine; //newLine would be a variable holding the endl function string += person.address;

(I am not sure if I understand the above code and how it works, however, or if I even did it right.) and then saved that string variable to the file, would customer >> store[i].name store the entire line or just until there is a white space?

Answers


input string using whilte space :

 #include <stdio.h>

int main()
{
 char string [256];
 printf ("Insert your full address: ");
 gets (string);     // warning: unsafe (see fgets instead)
 printf ("Your address is: %s\n",string);
 return 0;

}

concate String Like :

#include <stdio.h>
#include <string.h>

 int main ()
{
char str[80];
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
return 0;

}


Need Your Help

Function Fork in Linux Mint

c linux system fork mint

when I compile this code and I run it I get a result "PARENT" appears before the "CHILD".