데이터를 입력하는 텍스트 파일이 있지만 제대로 가져올 수 없습니다. 나는 텍스트 파일을로드하고 데이터를 입력 할 필요가 여기에C++, get 및 >> for ifstream 사용
Michael Davidson 153 Summer Avenue Evanston CO 80303
Ingrid Johnson 2075 Woodland Road Aurora IL 60507
코드입니다 :
여기 (이 실제 사람들이 걱정하지 마십시오하지 않습니다) 예를 들어 텍스트 파일에서 두 줄입니다 구조체에 넣습니다. 저는 C++을 처음 접했고 (분명히) 저는 get과 >>를 함께 사용하는 데 어려움을 겪고 있습니다. 내가 아래에있는 코드는 "상태"에 도달 할 때까지 제대로 작동합니다. 그런 다음 문제가 발생합니다. 도와 주셔서 감사합니다!
//constants
const int FIRST_NAME_LEN = 11;
const int LAST_NAME_LEN = 13;
const int ADDRESS = 25;
const int CITY_NAME_LEN = 16;
const int STATE_LEN = 3;
//define struct data types
struct CustomerType {
char firstName[FIRST_NAME_LEN];
char lastName[LAST_NAME_LEN];
char streetAddress[ADDRESS];
char city[CITY_NAME_LEN];
char state[STATE_LEN];
int zipCode;
};
//prototype function
ifstream& getInfo(CustomerType& CT_Struct, ifstream& infile);
int main() {
//declare struct objects
CustomerType CT_Struct;
ifstream infile("PGM951_customers.txt");
if(!infile) {
cerr << "Could not open the input file." << endl;
exit(1); //terminates the program
}
//call the function
getInfo(CT_Struct, infile);
return 0;
}
ifstream& getInfo(CustomerType& CT_Struct, ifstream& infile) {
while(infile) {
infile.get(CT_Struct.firstName, sizeof(CT_Struct.firstName));
infile.get(CT_Struct.lastName, sizeof(CT_Struct.lastName));
infile.get(CT_Struct.streetAddress, sizeof(CT_Struct.streetAddress));
infile.get(CT_Struct.city, sizeof(CT_Struct.city));
infile.get(CT_Struct.state, sizeof(CT_Struct.state));
infile >> ws;
infile >> CT_Struct.zipCode;
cout << CT_Struct.firstName << " | " << CT_Struct.lastName << " | " << CT_Struct.streetAddress
<< " | " << CT_Struct.city << " | " << CT_Struct.state << " | " << CT_Struct.zipCode << endl;
}
return infile;
}
=== 8 문자의 상태에서 편집 =========== 독서는 나에게 장난하고 미안 ... 다시 변경하는 것을 잊었다이었다.
"실제 사람들은 걱정하지 않아도됩니다." 이 사람들은 다른 사람을 구걸합니다 : http://en.wikipedia.org/wiki/Michael_Davidson;) –
하지만 그는 실제가 아니기 때문에 그의 의견은 중요하지 않습니다. . ;) – jalf
"무엇이 잘못되었는지"를 말하지 마십시오! 상태가 있지만 지퍼가 고장 나거나 상태가 잘못 되었나요? – crashmstr