Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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!
Posted 11-Mar-12 5:50am
MNMR408
Edited 11-Mar-12 6:16am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
 
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
  Permalink  
Comments
MNMR at 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?
johny10151981 at 11-Mar-12 12:16pm
   
I told you, you will have to read data this way
inFailas.read((char *) &itsName, SIZE);
MNMR at 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?
johny10151981 at 11-Mar-12 12:22pm
   
may be it would be plain
inFailas.read((char *)itsName, SIZE);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
Also there is no need to open the file as input and output in your outWrite() method.
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 11 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100