Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从具有不同变量类型的文件逐行读取_C++_File_File Io_Fault - Fatal编程技术网

C++ 从具有不同变量类型的文件逐行读取

C++ 从具有不同变量类型的文件逐行读取,c++,file,file-io,fault,C++,File,File Io,Fault,对于我正在做的一个项目,我们必须用“House”对象填充一个队列。“House”对象从名为“data.dat”的文件中获取信息。文件的每一行都是进入house对象的另一个对象。首先我取一个字符*作为地址,然后是一个int,另一个int,第三个int,然后是另一个char*。我们不会大声使用字符串来获取char*变量,我相信这就是我遇到问题的地方。每次编译时,它都告诉我有一个分段错误。这是我的queue.cpp区域,我非常确定错误在其中 #include"queue.h" #include<

对于我正在做的一个项目,我们必须用“House”对象填充一个队列。“House”对象从名为“data.dat”的文件中获取信息。文件的每一行都是进入house对象的另一个对象。首先我取一个字符*作为地址,然后是一个int,另一个int,第三个int,然后是另一个char*。我们不会大声使用字符串来获取char*变量,我相信这就是我遇到问题的地方。每次编译时,它都告诉我有一个分段错误。这是我的queue.cpp区域,我非常确定错误在其中

#include"queue.h"
#include<iostream>
#include<fstream>
#include<istream>

Queue::Queue(const char *filename){
    head = NULL;
    tail = NULL;

    std::ifstream infile(filename);

    char * address = NULL;
    int footage = 0;
    int bedrooms = 0;
    int bathrooms = 0;
    char * features = NULL;

    while(!infile.eof()){
            while(infile.get() != '\n'){
                    //std::cout << infile.get(); 
                    infile.get(address[i]);
            }
            infile >> footage >> bedrooms >> bathrooms;
            while(infile.get() != '\n'){
                    infile.get(features[i]);
            }

            enqueue(House(address, footage, bedrooms, bathrooms, features));
            }
    infile.close();
#包括“queue.h”
#包括
#包括
#包括
队列::队列(常量字符*文件名){
head=NULL;
tail=NULL;
std::ifstream infle(文件名);
字符*地址=空;
整数进尺=0;
内部卧室=0;
内部浴室=0;
char*features=NULL;
而(!infle.eof()){
而(infle.get()!='\n'){
//标准::cout>镜头>>卧室>>浴室;
而(infle.get()!='\n'){
获取(特征[i]);
}
排队(房屋(地址、录像、卧室、浴室、特色);
}
infle.close();
}

以下是房屋对象头文件:

    House();
    House(char * ad, int fo, int be, int ba, char * fe);
    char * getAddress();
    int getFootage();
    int getBedrooms();
    int getBathrooms();
    char * getFeatures();

    void setAddress(char * ad);
    void setFootage(int fo);
    void setBedrooms(int be);
    void setBathrooms(int ba);
    void setFeatures(char * fe);
    friend std::ostream& operator<<(std::ostream& out, House& house);

private:
    char * address;
    int footage;
    int bedrooms;
    int bathrooms;
    char * features;
House();
房屋(char*ad、int-fo、int-be、int-ba、char*fe);
char*getAddress();
int gettaines();
int getbeddrooms();
int getBathrooms();
char*getFeatures();
无效设置地址(字符*ad);
无效设置进尺(int-fo);
空立根柱面(int be);
休息室(内部ba);
无效设置特征(字符*fe);

friend std::ostream&operator您需要首先初始化
特性
地址
,方法是使用
新建
,或者将其创建为一个具有一定长度的字符数组。
按照这种方式,您试图写入尚未分配的内存-因此缓冲区溢出。

您需要首先初始化
功能
地址
,方法是使用
新建
,或将其创建为具有一定长度的字符数组。
按照这种方式,您试图写入尚未分配的内存-因此缓冲区溢出。

为了好玩,这里有一个已清理的版本

  • 它主要用
    std::string
    替换
    char*
    (因为我们在做C++)
  • 它使整个事情变得自给自足
  • 它使用正确的输入验证(不要在(!infle.eof())
时使用
,请检查提取运算符)

我没有实现您的队列:)

#include <iostream>
#include <fstream>
#include <sstream>

struct House {
    House()
        : address(), footage(0), bedrooms(0), bathrooms(0), features() 
    { }
    House(std::string ad, int fo, int be, int ba, std::string fe) 
        : address(ad), footage(fo), bedrooms(be), bathrooms(ba), features(fe) 
    { }

    std::string getAddress()   const { return address;   }
    int         getFootage()   const { return footage;   }
    int         getBedrooms()  const { return bedrooms;  }
    int         getBathrooms() const { return bathrooms; }
    std::string getFeatures()  const { return features;  }

    void setAddress(std::string  ad) { address   = ad; }
    void setFootage(int          fo) { footage   = fo; }
    void setBedrooms(int         be) { bedrooms  = be; }
    void setBathrooms(int        ba) { bathrooms = ba; }
    void setFeatures(std::string fe) { features  = fe; }

    friend std::ostream &operator<<(std::ostream &out, House const &house) {
        return out << "Address: "      << house.getAddress()   << '\n'
                   << "Footage: "      << house.getFootage()   << '\n'
                   << "Bed rooms:   "  << house.getBedrooms()  << '\n'
                   << "Bath rooms:   " << house.getBathrooms() << '\n'
                   << "Features: "     << house.getFeatures()  << '\n';
    }

  private:
    std::string address;
    int         footage;
    int         bedrooms;
    int         bathrooms;
    std::string features;
};

struct Queue {
    Queue(std::string  filename);

    struct Node {
        House value;
        Node* next;
    };

    Node *head, *tail;

    void enqueue(House const& h) {
        // TODO
        std::cout << h << "\n";
    }
};

Queue::Queue(std::string filename) : head(nullptr), tail(nullptr) {

    std::ifstream infile(filename);

    std::string address;
    int footage = 0;
    int bedrooms = 0;
    int bathrooms = 0;
    std::string features;

    std::string line; // lines

    while (getline(infile, address) && getline(infile, line)) {
        std::istringstream iss(line);

        if (iss >> footage >> bedrooms >> bathrooms && getline(iss, features)) {
            enqueue(House(address, footage, bedrooms, bathrooms, features));
        }

    }
}


int main()
{
    Queue q("data.dat");
}
它打印输出:

Address: Blv. Dreams Abroken 24, 78377d XG, ClassyCode
Footage: 2
Bed rooms:   4
Bath rooms:   1
Features:  pool sauna porch indoor-parking

Address: Oyi. Qernzf Noebxra 24, 78377q KT, PynfflPbqr
Footage: 3
Bed rooms:   8
Bath rooms:   2
Features:  cbby fnhan cbepu vaqbbe-cnexvat

为了好玩,这里有一个清理版本

  • 它主要用
    std::string
    替换
    char*
    (因为我们在做C++)
  • 它使整个事情变得自给自足
  • 它使用正确的输入验证(不要在(!infle.eof())
时使用
,请检查提取运算符)

我没有实现您的队列:)

#include <iostream>
#include <fstream>
#include <sstream>

struct House {
    House()
        : address(), footage(0), bedrooms(0), bathrooms(0), features() 
    { }
    House(std::string ad, int fo, int be, int ba, std::string fe) 
        : address(ad), footage(fo), bedrooms(be), bathrooms(ba), features(fe) 
    { }

    std::string getAddress()   const { return address;   }
    int         getFootage()   const { return footage;   }
    int         getBedrooms()  const { return bedrooms;  }
    int         getBathrooms() const { return bathrooms; }
    std::string getFeatures()  const { return features;  }

    void setAddress(std::string  ad) { address   = ad; }
    void setFootage(int          fo) { footage   = fo; }
    void setBedrooms(int         be) { bedrooms  = be; }
    void setBathrooms(int        ba) { bathrooms = ba; }
    void setFeatures(std::string fe) { features  = fe; }

    friend std::ostream &operator<<(std::ostream &out, House const &house) {
        return out << "Address: "      << house.getAddress()   << '\n'
                   << "Footage: "      << house.getFootage()   << '\n'
                   << "Bed rooms:   "  << house.getBedrooms()  << '\n'
                   << "Bath rooms:   " << house.getBathrooms() << '\n'
                   << "Features: "     << house.getFeatures()  << '\n';
    }

  private:
    std::string address;
    int         footage;
    int         bedrooms;
    int         bathrooms;
    std::string features;
};

struct Queue {
    Queue(std::string  filename);

    struct Node {
        House value;
        Node* next;
    };

    Node *head, *tail;

    void enqueue(House const& h) {
        // TODO
        std::cout << h << "\n";
    }
};

Queue::Queue(std::string filename) : head(nullptr), tail(nullptr) {

    std::ifstream infile(filename);

    std::string address;
    int footage = 0;
    int bedrooms = 0;
    int bathrooms = 0;
    std::string features;

    std::string line; // lines

    while (getline(infile, address) && getline(infile, line)) {
        std::istringstream iss(line);

        if (iss >> footage >> bedrooms >> bathrooms && getline(iss, features)) {
            enqueue(House(address, footage, bedrooms, bathrooms, features));
        }

    }
}


int main()
{
    Queue q("data.dat");
}
它打印输出:

Address: Blv. Dreams Abroken 24, 78377d XG, ClassyCode
Footage: 2
Bed rooms:   4
Bath rooms:   1
Features:  pool sauna porch indoor-parking

Address: Oyi. Qernzf Noebxra 24, 78377q KT, PynfflPbqr
Footage: 3
Bed rooms:   8
Bath rooms:   2
Features:  cbby fnhan cbepu vaqbbe-cnexvat

data.dat看起来像…?data.dat看起来像。。。?