文件流读取错误 [英] file stream read error

查看:127
本文介绍了文件流读取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

需要你的帮助.在第二次运行已编译源代码后尝试读取文件中的书面信息时,为什么会出现分段错误错误"?好吧,我的程序示例必须将对象数据成员写入文件.后来,当我运行程序并从文件中选择方法读取名称时,我想看到此书面名称.谁能解释我的错误在哪里?这是我的原始示例:

Good day people!

Need your help. Why I get "Segmentation fault error" when I try to read written information in file after second runtime of my compiled source? Well, my program example must write object data member to file. And later, when I run my program and select method read name from file, I wanna see this written name. Anybody can explain where is my mistake? Here is my source example:

// main.cpp
#include <iostream>
#include <fstream>
#include <string>


class cHuman
{
	private:
			std::string itsName;
	public:
			cHuman():itsName("Peter"){}    // my default constructor
			~cHuman(){}    // destructor
			
			std::string getName() const { return itsName; }    // my accessors
			void setName(std::string name) { itsName = name; }
			
			void outWrite();   // methods to read and write info to file
			void inRead();
};

// method implementation

void cHuman::outWrite()
{	
	std::ifstream inFailas("data.db", std::ios::in | std::ios::binary);
	std::ofstream outFailas("data.db", std::ios::out | std::ios::binary);
	
	if(!inFailas)
	{
		std::cout << "Error! File data.db not found!";
	}
	else
	{		
		cHuman man;
		std::cin >> itsName;
		outFailas.write((char *) &man, sizeof(cHuman));
		outFailas.close();
		inFailas.close();
	}
}

void cHuman::inRead()
{
	std::ifstream inFailas("data.db", std::ios::in | std::ios::binary);
	
	if(!inFailas)
	{
		std::cout << "Error! File data.db not found!";
	}
	else
	{	
		cHuman man;
		inFailas.read((char *) &man, sizeof(cHuman));
		std::cout << getName();
		inFailas.close();
	}
}



int main()
{
        cHuman man;    // local object declaration

	std::cout << "Choose method: " << std::endl;
	std::cout << "(1) - Write, (2) - Read" << std::endl;
	
	unsigned short int choose;
	
	std::cin >> choose;
	// menu to read or to write info to file
	if(choose == 1)
	{
		man.outWrite();
	}
	else if(choose == 2)
	{
		man.inRead();
	}
	else
	{
		return 1;
	}
	
	return 0;
}



谢谢您的任何答复!



Thank you for any response!

推荐答案

您将man声明为global,man也声明为local,容易出错的编程风格

除此之外,您还没有告诉您细分的结果

现在看一下代码块
You declared man as global and man as local too, error prone programming style

besides this you didnt tell where you get your segmentation falut

now look at the code block
cHuman man;
		inFailas.read((char *) &man, sizeof(cHuman));
		std::cout << itsName;
		inFailas.close();



一切都错了,

man是cHuman类的对象,但是您正在向man读取数据.

读取 itsName 变量中的数据,而不是 man

还学习如何调试.这很重要.我猜您正在使用linux. leran如何使用gdb



everything is wrong about it,

man is a object of cHuman class but you are reading data to man.

read data in itsName variable istead of man

Also learn how to debug. its very important. I guess you are using linux. leran how to use gdb


您不应该在inRead()outWrite()函数中创建和使用本地cHuman对象.这段代码是该类的一部分,并且在调用时应读取和写入存在于该类实例中的变量,例如:
You should not be creating and using a local cHuman object within your inRead() and outWrite() functions. This code is part of the class and when called should be reading and writing the variables that exist within this instance of the class, something like:
std::cin >> itsName;
outFailas.write(itsName.c_str(), strlen(itsName.c_str()));
outFailas.close();


同样,也无需在您的outWrite()方法中打开文件作为输入和输出.


Also there is no need to open the file as input and output in your outWrite() method.


这篇关于文件流读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆