Click here to Skip to main content
15,121,230 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
// 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!
Posted
Updated 11-Mar-12 7:16am
v4

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
C++
cHuman man;
		inFailas.read((char *) &man, sizeof(cHuman));
		std::cout << itsName;
		inFailas.close();


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
   
Comments
MNMR 11-Mar-12 12:13pm
   
Ok, thanks for your observation, maybe global and local declaration is error my prone programming style. I can correct it. But when I try to read from file my object method man.getName(); I get the same error, maybe it's read anyway from object, but not from file?
Mohibur Rashid 11-Mar-12 12:16pm
   
I told you, you will have to read data this way
inFailas.read((char *) &itsName, SIZE);
MNMR 11-Mar-12 12:20pm
   
Ok, now it clearer a little. I'll test. Thank you. But what about this stream to show in screen?
Mohibur Rashid 11-Mar-12 12:22pm
   
may be it would be plain
inFailas.read((char *)itsName, SIZE);
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:
C++
std::cin >> itsName;
outFailas.write(itsName.c_str(), strlen(itsName.c_str()));
outFailas.close();

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900