2012-11-28 4 views
-2

나는 약간의 물건을 알아 냈습니다.하지만 fstreams를 빨아 들였습니다. BREED + DESC를 위해, 나는 그들과 이름 사이에 공백이 있지만, 완전히 제거하면 공백이 없어 다시 작동합니다. 누군가 내가 뭘 잘못하고 있다고 말할 수 있습니까? 사람들이 (이미이 질문의 이전 버전에 대한 응답으로) 언급으로Fstreams C++에는 쓰기에 여분의 공백이 있습니다.

#include <iostream> 
#include <fstream> 
#include <cstring> 
#include <cctype> 
using namespace std; 


const int NAME_SIZE = 100; 
const int BREED_SIZE = 100; 
const int DESC_SIZE = 250; 
const int REASON_SIZE = 250; 

//creating the struct 
struct animal 
{ 
//members 
    char name[NAME_SIZE]; 
    char breed[BREED_SIZE]; 
    char desc[DESC_SIZE]; 
    char reason[REASON_SIZE]; 
    float age; 
    float weight; 
    int day; 
    int month; 
    int year; 
    float length; 
}; 

class petAdoption 
{ 
    public: 
     petAdoption(); 
     void enroll(animal newAnimal[], int & count); 
     void read(animal newAnimal[], int & count); 
     //void display(animal & newAnimal); 
     //void adopt(animal & newAnimal); 
    private: 


}; 
petAdoption::petAdoption() 
{ 

} 

int main() 
{ 
int count = 0; 
animal * newAnimal = new animal[count]; 
petAdoption adopt; 
adopt.read(newAnimal, count); 
adopt.enroll(newAnimal, count); 
delete[] newAnimal; 
} 


void petAdoption::read(animal newAnimal[], int & count) 
{ 
    int pets = 0; 
    ifstream read; 
    read.open("pets.txt"); 
    if(!read) 
    { 
     cout << "Checking... File doesn't exist!" <<endl; 
    } 
    else 
    { 
     while(!read.eof()) 
     { 
     read.getline(newAnimal[pets].name, NAME_SIZE, '\n'); 
     read.ignore(100, '\n'); 
     ++pets; 
     } 
    count = pets + 1; 
    } 
    read.close(); 

    for (int i = 0; i < pets; ++i){ 
     read.open(newAnimal[i].name); 
      if(!read) 
      { 
       cout << "Checking... File doesn't exist!" <<endl; 
      } 
     else{ 
      while(!read.eof()) 
      { 
       read.getline(newAnimal[i].name, NAME_SIZE, '\n'); 
       read.getline(newAnimal[i].breed, BREED_SIZE, '\n'); 
       read.getline(newAnimal[i].desc, DESC_SIZE, '\n'); 
       read.getline(newAnimal[i].reason, REASON_SIZE, '\n'); 
       read.ignore(100, '\n'); 
       read >> newAnimal[i].age; 
       read >> newAnimal[i].weight; 
       read >> newAnimal[i].day; 
       read >> newAnimal[i].month; 
       read >> newAnimal[i].year; 
       read >> newAnimal[i].length; 
       read.ignore(100, '\n'); 
      } 
     } 
     read.close(); 
    } 
} 


/* 
ENROLL FUNCTION 
*/ 

void petAdoption::enroll(animal newAnimal[], int & count) 
{ 
//making a write variable 
    ofstream write; 

//stores the name into the struct member 
    cout << "Please enter a name: "; 
    cin.get(newAnimal[count].name, NAME_SIZE, '\n'); 
    cin.ignore(100, '\n'); 
    cout << "Please enter a breed: "; 
    cin.get(newAnimal[count].breed, BREED_SIZE, '\n'); 
    cin.ignore(100, '\n'); 
    cout << "Please enter a desc: "; 
    cin.get(newAnimal[count].desc, DESC_SIZE, '\n'); 
    cin.ignore(100, '\n'); 
    cout << "Please enter a reason (EG: eats people): "; 
    cin.get(newAnimal[count].reason, REASON_SIZE, '\n'); 
    cin.ignore(100, '\n'); 
    cout << "Please enter an age (EG: 5): "; 
    cin >> newAnimal[count].age; 
    cin.ignore(100, '\n'); 
    cout << "Please enter a weight (in LBS): "; 
    cin >> newAnimal[count].weight; 
    cin.ignore(100, '\n'); 
    cout << "Please enter a day (EG: 2): "; 
    cin >> newAnimal[count].day; 
    cout << "Please enter a month (EG: 11): "; 
    cin >> newAnimal[count].month; 
    cout << "Please enter a year (EG: 2002): "; 
    cin >> newAnimal[count].year; 
    cout << "How long has the pet been in a shelter? (in months): "; 
    cin >> newAnimal[count].length; 


//error 
    if(!write) 
    { 
     cout << "Invalid File" << endl; 
    } 
    else{ 
     write.open(newAnimal[count].name, ios::app);  
     write << newAnimal[count].name << '\n' 
     << newAnimal[count].breed << '\n' 
     << newAnimal[count].desc << '\n' 
     << newAnimal[count].reason << '\n' 
     << newAnimal[count].age << '\n' 
     << newAnimal[count].weight << '\n' 
     << newAnimal[count].day << '\n' 
     << newAnimal[count].month << '\n' 
     << newAnimal[count].year << '\n' 
     << newAnimal[count].length << '\n'; 
     write.close(); 
    } 
    if(!write) 
    { 
     cout << "Invalid File"; 
    } 
    else { 
     write.open("pets.txt", ios::app); 
     write << newAnimal[count].name << '\n'; 
     write.close(); 
    }    
} 
+1

왜'std :: string'이 아닌'char' 배열을 사용하고 있습니까? – NPE

+1

\ * yawn \ *'while (! read.eof())'이 잘못되었습니다. 어떤 자원이 사람들에게 이것을 가르치고 있습니까? –

+0

명확하게 언급 할 수 있습니까? 문제는 무엇입니까? – cppcoder

답변

1

당신은 당신의 루프를 제어 할 수 eof()를 사용하지 않아야한다. 또한 [읽으려고 시도한 후] 입력을 성공적으로 받았는지 테스트해야합니다.

행이 getline()이 아닌 get()이 아니기 때문에 줄 바꿈을하는 경우 마지막 줄에 문제가 발생합니다. 이전 줄 문자는 중지 문자를 유지하지만 후자는 그렇지 않습니다. std::stringstd::getline()과 함께 사용하는 것이 좋습니다. 임의의 길이 문자열을 읽습니다.

+0

문자 배열을 사용해야합니다. 또한 getline을 사용할 때 같은 문제가 발생합니다. 그래서 나는 내가 시험해 볼 것이다라고 생각했다. – user1858740

+0

아니요, 문자 배열을 사용하지 않아도됩니다. –

+0

두 가지 : 1. 파일에 계속 추가하기 때문에 파일이 처음에는 비어 있고 처음 쓴 이후 해결 된 문제를 디버깅하지 않는지 확인하십시오. 2. 문제를 보여주는 ** 작은 ** 예를 만듭니다. 즉, 코드를 복사하고 문제를 나타내는 코드 만 존재할 때까지 모든 것을 제거하십시오. 나는 그것이 당신의 경우에 20 라인 이상이면 안된다. ... 그리고'get()'과'getline()'은 분명히 내가 말한 것처럼 다른 방식으로 행동한다. –

관련 문제