簡體   English   中英

從 txt 文件中讀取變量並將它們作為 arrays c++ 保存到 class 對象中

[英]Reading variables from txt file and saving them into the class objects as arrays c++

我試圖從一個看起來像的 txt 文件中獲取變量;

1 Prince Heins 25
2 Lady Bridgette 29
3 Tony Ann 223
4 Lucy Phoenix 35

這是我的代碼,但我無法將變量從 txt 文件獲取到 myArray。 我通過使用 getline 和 linenumber 等於linecount創建了它們的行數。 然后我創建了許多等於行數的對象。 下一步是逐行從 txt 文件中獲取變量,我嘗試使用 while(pbin),這是我讀取的 function。 我正在嘗試通過使用包含我的字符串和 integer 的設置函數將變量發送到 class 但我得到一個編譯錯誤: [錯誤]不匹配'operator>>'(操作數類型是'std :: ifstream {aka std:: basic_ifstream}' 和 'void')我檢查了示例,但沒有一個解決了我的問題。 無法解決問題並修復它。 如果有人提供幫助,我會很高興。

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>

using namespace std;

class contact{
private:

    int listno;
    string name;
    string surname;
    string phonenumber;
        
public:
    contact(){
        this->name="Unknown";
        this->surname="Unknown";
        this->phonenumber="Unknown";
        this->listno=0;
    }
    contact (string name,string surname,string phonenumber){
        this->name=name;
        this->surname=surname;
        this->phonenumber=phonenumber;
        this->listno=0;
    }
    contact(int listno,string name,string surname,string phonenumber){
        
        this->name=name;
        this->surname=surname;
        this->listno=listno;
        this->phonenumber=phonenumber;
    }
    void setListno(int listno){
        this->listno=listno;
    }
    
    int getListno(){
        return listno;
    }
    
    void setName(string name){
        this->name=name;
    }
    
    string getName(){
        return name;
    }
    
    void setSurname(string surname){
        this->surname=surname;
    }
    
    string getSurname(){
        return surname;
    }
    
    void setNumber(string phonenumber){
        this->phonenumber=phonenumber;
    }
    
    string getNumber(){
        return phonenumber;
    }
    
    
    void showInfos(){
        cout << "Listno: " << this->listno << endl;
        cout << "Name: " << this->name << endl;
        cout << "Surname: " << this->surname << endl;
        cout << "Number: " << this->phonenumber<<endl;
        
    }
    

};



int main(){
    int mListno;
    string mName;
    string mSurname;
    string mNumber;
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount;
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            while(
            pbin >> myArray[i].setListno(mListno);
            pbin >> myArray[i].setName(mName);
            pbin >> myArray[i].setSurname(mSurname);
            pbin >> myArray[i].setNumber(mNumber);
            )

        }
    }
    pbin.close();
    
    return 0;
}

編輯:

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>

using namespace std;

class contact{
private:

    int listno;
    string name;
    string surname;
    string phonenumber;
        
public:
    contact(){
        this->name="Unknown";
        this->surname="Unknown";
        this->phonenumber="Unknown";
        this->listno=0;
    }
    contact (string name,string surname,string phonenumber){
        this->name=name;
        this->surname=surname;
        this->phonenumber=phonenumber;
        this->listno=0;
    }
    contact(int listno,string name,string surname,string phonenumber){
        
        this->name=name;
        this->surname=surname;
        this->listno=listno;
        this->phonenumber=phonenumber;
    }
    void setListno(int listno){
        this->listno=listno;
    }
    
    int getListno(){
        return listno;
    }
    
    void setName(string name,int count){
        this->name=name[count];
    }
    
    string getName(){
        return name;
    }
    
    void setSurname(string surname){
        this->surname=surname;
    }
    
    string getSurname(){
        return surname;
    }
    
    void setNumber(string phonenumber){
        this->phonenumber=phonenumber;
    }
    
    string getNumber(){
        return phonenumber;
    }
    
    
    void showInfos(){
        cout << "Listno: " << this->listno << endl;
        cout << "Name: " << this->name << endl;
        cout << "Surname: " << this->surname << endl;
        cout << "Number: " << this->phonenumber<<endl;
        
    }
    
    friend istream & operator>> (istream & in, contact & con){
        in >> con.listno >> con.name >> con.surname >> con.phonenumber;
        return in;
    }

};





int main(){
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount=0;
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    pbin.seekg(0);
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            if(! (pbin >> myArray[i]))
            {
                cout << "The contact is not found." ;
            }
        }
    }
    pbin.close();
    
    
    return 0;
}

添加到contact >>超載

    friend std::istream & operator>> (std::istream & in, contact & con)
    {
        in >> con.listno >> con.name >> con.surname >> con.phonenumber;
        return in;
    }

然后在main中, for循環變為

        for(i=0;i<linecount;i++){
            if (! (pbin >> myArray[i]))
            { // failed to read a contact
                // do something here to clean up the mess
            }
        }

為了彌補其他幾個錯誤,

int main(){
    ifstream pbin("phoneData2.txt");
    string line;
    long linecount = 0; // otherwise you don't know what number linecount starts at
    contact* myArray = new contact[linecount];
    for(linecount=0;getline(pbin,line);linecount++);
    pbin.clear(); // Clear the error flag from hitting the end of the file
                  // Needed when building to older C++ Standards
    pbin.seekg(0); // rewind the file
    if(pbin.is_open()){
        int i;
        for(i=0;i<linecount;i++){
            if (! (pbin >> myArray[i]))
            {
                // do something here to clean up the mess
            }

        }
    }
    pbin.close();

    return 0;
}


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM