好吧,伙计们!写入文件时遇到问题。基本上,我的问题是,当我写入文件时,例如变量2913 Harvey Drive
中的person.address1
等地址将被写入文件中的单独行而不是一行。我需要他们在一条线上。我做了一些研究,找不到任何我理解的东西,所以我希望我能有一个例子,因为它与我的代码有关,以帮助我理解。这是我的代码:
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;
}
我看到连接字符串变量然后将ENTIRE字符串变量保存到文件中的内容,但我不确定我是否会理解如何读取每一行。这是读取代码的函数:
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;
}
因此,如果我使用了连接方法,例如:
string += person.name;
string += newLine; //newLine would be a variable holding the endl function
string += person.address;
(我不确定我是否理解上述代码及其工作原理,或者我是否做得对。)
然后将string
变量保存到文件中,customer >> store[i].name
会存储整行还是直到有空格?
答案 0 :(得分:0)
使用空格输入字符串:
#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;
}
concat 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;
}