从具有不同变量类型的文件中逐行读取
Reading in line by line from a file with different variable types
对于我正在进行的项目,我们必须用"House"对象填充队列。"House"对象从一个名为"data.dat"的文件中获取信息。文件的每一行都是进入House对象的另一个东西。因此,首先我取一个char*作为地址,然后是一个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();
}
这是房子对象头文件:
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;
};
您需要首先初始化features
和address
,可以使用new
,也可以将其创建为一定长度的字符数组。这样做的方式是试图写入尚未分配的内存,从而导致缓冲区溢出。
为了好玩,这里有一个经过清理的版本
- 主要是用
std::string
代替char*
(因为我们在做C++) - 它使整个事情变得独立
- 它使用正确的输入验证(不要使用
while (!infile.eof())
,检查提取运算符)
我没有实现您的队列:)
在Coliru上直播
#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");
}
用于输入:
Blv. Dreams Abroken 24, 78377d XG, ClassyCode
2 4 1 pool sauna porch indoor-parking
Oyi. Qernzf Noebxra 24, 78377q KT, PynfflPbqr
3 8 2 cbby fnhan cbepu vaqbbe-cnexvat
它打印输出:
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
相关文章:
- 如何在c++中从文本文件中逐行读取整数
- 打印逐行存储的文本文件
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 逐行读取文件,并将数据插入变量和数组中
- 逐行从文本文件中读取 |C++
- 我遇到使用 c++ 逐行读取输入文件的问题
- 无法逐行读取.csv文件
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- C ++ txt 文件成数组逐行字符串和 int 用于纸牌游戏
- 如何将文件逐行读取到矢量中,然后打印矢量
- C++逐行读取文件
- 如何逐行读取文件并分隔行组件?
- C++ 逐行从文件(包含空格)读取数据
- 对包含整数的文本文件进行排序时,必须逐行进行排序
- C++ 循环逐行写入文件还是添加到数组并在循环后写入文件?
- 将 1 和 0 的文件逐行加载到 char** 中
- 从文件逐行加载2D矢量时出现分割错误
- c++将一个TXT文件逐行读入结构数组中的字符数组
- 将文件逐行输入到结构中