This question already has answers here:
Why does C++ reject appending a structure to a binary file if it contains a member of the string class?
(2 answers)
Closed 5 years ago.
I've written a small code in c++ to understand data file handling. The program consists of accepting entries, writing them to a .dat file and searching for a particular entry. So far, only the writing function is working, the reading and the searching function give a segmentation fault error. What's going wrong?
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class data_base
{
string name;
long int no;
public:
void input()
{
cout<<"\nEnter name:";
cin>>name;
cout<<"Enter ph number:";
cin>>no;
}
void display()
{
cout<<name<<"\t"<<no;
}
string retname()
{
return name;
}
long int retno()
{
return no;
}
};
void display_all()
{
data_base d;
fstream in;
in.open("database.dat",ios::in|ios::binary);
if(!in.is_open())
cout<<"Error opening file";
else{
while(in.read((char *)&d,sizeof(d)))
{
d.display();
}
}
in.close();
}
void search_name()
{
data_base d;
fstream in;
string s;
in.open("database.dat",ios::in|ios::binary);
if(!in.is_open())
cout<<"Error opening file";
else{
cout<<"\nEnter name to be searched:";
cin>>s;
while (in.read((char *) &d,sizeof(d))) {
if(s==d.retname())
{
d.display();
}
}
}
in.close();
}
void search_no()
{
data_base d;
fstream in;
long int l;
in.open("database.dat",ios::in|ios::binary);
if(!in.is_open())
cout<<"Error opening file";
else{
cout<<"\nEnter number to be searched:";
cin>>l;
while (in.read((char *) &d,sizeof(d)))
{
if(l==d.retno())
{
d.display();
}
}
}
in.close();
}
int main()
{
int ch;
fstream file;
data_base d,e;
string s;
while(1)
{
cout<<"1.Add entry\n2.Search by name\n3.Search by
number\n4.Display all entries\5.Exit"<<endl;
cin>>ch;
switch (ch) {
case 1: d.input();
file.open("database.dat",ios::out|ios::app|ios::binary);
if(!file.is_open())
cout<<"Error opening file";
else
{
file.write((char *)&d,sizeof(d));
cout<<"Entry added!"<<endl;
}
file.close();
break;
case 2:search_name();
break;
case 3:search_no();
break;
case 4:display_all();
break;
case 5: exit(0);
}
}
}
class data_base
{
string name;
...
}
...
data_base d;
...
file.write((char *)&d,sizeof(d));
You are already doomed here. The std::string struct has a pointer inside. Reinterpreting this pointer as a char array gives you some sequence of chars. You save it to a file and then read it some time later.
At that moment your newly read data_base variable has exact binary representation as the one before. But now the internal pointer of std::string no longer points to a valid memory address, hence segfault.
Note that segfaulting is actually good. In some bad case the memory address would be valid but it would point to some completely unrelated (random) data. That would be a bug extremely hard to find.
You are doing this wrong from the begining. Don't cast to/from char* for serialization/deserialization of data. Use proper serialization techniques, e.g. convert the structure to xml, json, google protobuf or even come up with something custom. Just don't reinterpret raw memory. Even if there are no pointers inside your struct the binary representation is compiler, os and cpu dependent.
Related
I've been doing my project and the last thing I need to do is to save to and start reading a structure array from a file on startup of the program, but I can't figure out why the code isn't loading the information of the file. I know that it does save something since I can open the .dat file and read it in a text editor.
I apologize for the terrible style, but I'm still new. That's a sample of just that function in the code:
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
struct property {
int num;
char nBrok[50];
char type[10];
string adress;
char outlook[20];
double price;
double size;
int nRooms;
int floor;
int status;
};
fstream fp;
void fileWrite(property bDanni[], int n) {
fp.open("dbase.dat", ios::binary | ios::out);
if (!fp) {
cout << "\n Error in file \n"; exit(1);
}
fp.write((char*)bDanni, sizeof(property) *n);
fp.close();
}
int fileRead(property bDanni[]) {
long pos; int n = 0, i; property b;
fp.open("dbase.dat", ios::binary | ios::in);
if (!fp) {
cout << "\n file does not exist\n"; return n;
}
fp.seekg(0l, ios::end);
pos = fp.tellg();
fp.close();
n = pos / (sizeof(property));
fp.open("dbase.dat", ios::binary | ios::in);
if (!fp) {
cout << "\n Error in file \n"; exit(1);
}
for (i = 0; i < n; i++) {
fp.read((char*)&b, sizeof(property));
bDanni[i] = b;
}
fp.close();
return n;
}
int main() {
property bDanni[100];
char answer;
int total = 0;
cout << "Do you wat to read from the save file?(y/n): ";
cin >> answer;
if (answer == 'y') {
int total = fileRead(bDanni);
}
}
The problem is that a C++ std::string is much more complex than a char array. The implementation is not mandated by the standard, but in common implementation, the string element contains a pointer to a character array. That means that your code only stores into the file a pointer value instead of a character string.
In C++ idiom, the std::string type is said not to be trivially copyable. And the fread-fwrite method can only be used with trivially copyable types.
That means that you will have to use serialization to replace the raw byte representation of a std::string with a sequence of bytes that represent the useful content of the object, something that you will be able to use at read time to construct back the object. Not really complex but beyond a mere fwrite.
First time execution is success without any error but when it creates Data.bin I get some error when I use fread.
Sorry I don't know how to ask it so please look at the program.
I commented the error making statement.
#include<iostream>
#include<vector>
using namespace std;
typedef struct myStuff
{
int cdno;
string content,des;
}MS;
int main()
{
vector<MS> data;
int i=0;
string in;
FILE *fr=NULL,*fw=NULL;
fr=fopen("Data.bin","rb");
//----------------------------------------------------------------------
if(fr!=NULL)
{
do
{
data.resize(++i);
}while( fread(&data[i-1],sizeof(MS),1,fr) ); //ERROR
fclose(fr);
}
else
data.resize(++i);
//----------------------------------------------------------------------
while(1)
{
cout<<"Enter x to exit or c to continue updating data: ";
cin>>in;
if(in=="x"||in=="X")
{
fw=fopen("Data.bin","wb");
fwrite(&data[i],sizeof(MS),i,fw);
fclose(fw);
exit(0);
}
else if(in=="c"||in=="C")
{
cout<<"Enter CD no: ";
cin>>data[i-1].cdno;
cout<<"Enter Contents: ";
cin>>data[i-1].content;
cout<<"Enter Description: ";
cin>>data[i-1].des;
data.resize(++i);
}
else
cout<<"Try Again..."<<endl;
}
}
you shouldn't use sizeof(string) or sizeof a structure that contains a string, that's meaningless, it just gives you the compile time (static) size of the class string. you should instead use string.size() which returns the dynamic size of the string.
I have a small project for a C++ course and I'm stuck trying to check if a value of a data member of STUDENT's class exists in the file(the "ID"). I've tried to use some function that I found on the internet to transform the integer value I'm searching for into a string and then use the find function, to search for it in each line of the file.
It works, but whenever I check one line from the file, it gets false pozitive, because the ID value(for example "12") is for example, identical to the value of age(also "12"). It does that because the age value comes before the ID value in my file and also in the string variable (and I can't change it). I don't know to search in the string for the value of ID only. I use the function "inputInfo" to input student1's member values from the keyboard, and function "checkID" to check if value of "ID" already exists in the file. Also, for another aspect of the project, I am seeking a way to search for occurrence of the ID and name data members values in the same file(once they are already written). One solution I've thought is to somehow start the search after the occurence of another character(for example the space character, given the fact that in the file, each field is delimited from another with a space), but I'm not sure the find function is able to do that.Thank you in advance for your help.Below is a part of the project's code in C++:
#include<iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
int checkID(int idNumber)
{
string findID;
stringstream id_string;
id_string << idNumber;
findID = id_string.str();
int offset;
ifstream in;
in.open("Students.txt");
if(in.is_open())
{
string line;
while(getline(in, line))
{
if(offset = line.find(findID, 0)!= string::npos)
{
cout<<"The ID already exists. Insert a different ID!"<<endl;
return 0;
}
}
}
else
cout<<"File doesn't exist!"<<endl;
in.close();
}
class PERSON
{
protected:
string name;
string surname;
unsigned int age;
public:
void inputinfo()
{
cin>>name;
cin>>surname;
cin>>age;
}
outputinfo()
{
cout<<name<<endl;
cout<<surname<<endl;
cout<<age<<endl;
}
};
class STUDENT: public PERSON
{
int ID;
float marks_sum;
string belonging_class;
public:
inputInfo()
{
cout<<"Name:";
cin>>name;
cout<<"Surname:";
cin>>surname;
cout<<"Age:";
cin>>age;
do
{
cout<<"ID:";
cin>>ID;
}
while (checkID(ID)==0);
cout<<"Sum of marks:";
cin>>marks_sum;
cout<<"The belonging class:";
cin>>belonging_class;
}
void outputInfo()
{
cout<<name<<endl;
cout<<surname<<endl;
cout<<age<<endl;
cout<<ID<<endl;
cout<<marks_sum<<endl;
cout<<belonging_class<<endl;
}
friend std::ostream& operator << (std::ostream& os, const STUDENT& value )
{
os << value.name<<" "<<value.surname<<" "<<value.age<<" "<<value.ID<<" "<<value.marks_sum<<" "<<value.belonging_class<<std::endl;
return os;
}
};
STUDENT student1;
int writeInFile(STUDENT studentx)
{
ofstream os("Students.txt", ofstream::app);
os << studentx;
os.close();
}
int main()
{
int opt1, opt2;
char option;
do
{
cout<<"1 - Input data into file"<<endl<<"2 - Close program"<<endl;
cin>>opt1;
switch(opt1)
{
case 1:
do
{
cout<<endl;
cout<<"Choose one of variants"<<endl<<"1.Students"<<endl<<"2.Get back to main menu"<<endl;
cin>>opt2;
switch(opt2)
{
case 1:
do
{
cout<<"Do you wish to introduce a new student(Y/N)?";
cin>>option;
if(option!='N')
{
student1.inputInfo();
writeInFile(student1);
}
}
while (option!='N');
break;
}
}
while(opt2!=2);
break;
}
}
while(opt1!=2);
}
#include <sstream>
using namespace std;
bool isUniqueID(ifstream& file, int id)
{
string id_string = to_string(id);
string currently_read_line;
// The position of the searched key. So, in this case,
// only the 3rd value will be tested (starting from 0).
// John Doe 23 456
// | | | |
// 0 1 2 3 (the id)
int offset = 3;
while (getline(file, currently_read_line))
{
istringstream ss(currently_read_line);
string current_entry;
int counter = 0;
while (ss >> current_entry) {
if (current_entry == id_string && counter == offset) {
cout << "The Id already exists." << endl;
return false;
}
counter++;
}
}
// No match found
cout << "The ID does not exist yet." << endl;
return true;
}
Please note:
Just pass your opened file to the function. The file is opened once, instead of opening it every time you want to check an ID.
This requires to compile in -std=c++11 (for the to_string conversion)
[Update]
The offset variable tells the function what value to test for. A more consistent way to do this, would be to format the data as to have a key/value for each student entry. It works as it though.
I have my prototypes in a header file, but I need some help. I am having some trouble getting the program to compile all the way through. It appears to be getting caught in a loop with the input. Possibly some issues with the functions. Thanks in advance for any input.
#include <iostream>
#include <conio.h>
#include "header.h"
#include <fstream>
class Caesar
{
public: void readText(char *input);
void encrypt(char *input,char *output,char *key);
void decrypt(char *input,char *output,char *key);
};
void main()
{
Caesar a;
char key[1000];
ifstream fin;
int choice;
char input[100],output[100];
cout<<"\n Enter input file: ";
cin>>input;
cout << input;
cout<<"\n Enter output file: ";
cin>>output;
cout <<output;
cout<<"\n Enter key: ";
cin>>key;
cout <<key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): "<< endl;
cin >> choice;
cout << choice;
a.readText(input);
if(choice==1)
{
a.encrypt(input,output,key);
}
if(choice==2)
{
a.decrypt(input,output,key);
}
else
{
cout<<"\n\n Unknown choice";
}
}
void Caesar::readText(char *input)
{
ifstream reader;
char buf;
reader.open(input);
cout<<"\n\n <--- "<<input<<" --->\n";
buf=reader.get();
while(!reader.eof())
{
cout<<buf;
buf=reader.get();
}
reader.close();
}
void Caesar::encrypt(char *input,char *output,char *key)
{
ifstream reader;
ofstream writer;
char buf;
reader.open(input);
writer.open(output);
buf=reader.get();
while(!reader.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key[buf];
buf%=26;
buf+='A';
}
writer.put(buf);
buf=reader.get();
}
reader.close();
writer.close();
readText(input);
readText(output);
}
void Caesar::decrypt(char *input,char *output,char *key)
{
ifstream reader;
ofstream writer;
char buf;
reader.open(input);
writer.open(output);
buf=reader.get();
while(!reader.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key[buf];
buf%=26;
buf+='a';
}
writer.put(buf);
buf=reader.get();
}
reader.close();
writer.close();
readText(input);
readText(output);
}
if(choice=1)
should be
if(choice==1)
and also in the other if
In your case, you are assigning the value 1 to choice, then test if choice is true, and it is, since any non-zero numeral type is implicitly casted to bool true.
I have just executed your code and tried debugging it and took a screen shot
you program gets into a loop after entering the choice.there is no problem with cin>>.
From your comments, it seems like you're just trying to debug main. Everything seems to work fine. What are you inputting for key? If it's a very large integer, that may be your problem as it might exceed the maximum integer range and cause overflow.
Your key is an integer variable. You are inputting a string for the file name that holds your key, so that should be changed to a C string array. Change all of the passed key parameters to char* instead of int.
You have an infinite loop when the readText() function is called.
Maybe try this:
void Caesar::readText(char *input)
{
ifstream reader(input);
if(reader.is_open())
{
char buf;
cout<<"\n\n <--- "<<input<<" --->\n";
while(reader.get(buf))
{
cout << buf;
}
}
reader.close();
}
Make sure that your text file is in the same folder as your code. See this for more details: ifstream not opening file
The following code read 3 obj and write them into a file.
however im unable to retrieve objects properly using the below code.
data is duplicated and is not in order
plz help
old code :
#include<fstream.h>
#include<conio.h>
class mail
{
public:
char un[25]; // user name
char pd[25]; // passsword
void reg(int);
} obj[5];
void mail::reg(int k)
{
int i;
i=k;
clrscr();
cout<<"Enter user name ( enter unique name )\n";
cin>>un;
cout<<"Enter password\n";
cin>>pd;
ofstream filout;
filout.open("email",ios::app||ios::binary);
if(!filout)
{
cout<<"cannot open file\n";
}
else
{
cout<<"\n "<<i;
filout.write((char *)&obj[i],sizeof(mail));
filout.close();
}
cout<<"You are now registered. \n";
getch();
} // end of sign up or register func
void main()
{
int t;
clrscr();
obj[0].reg(0);
obj[1].reg(1);
obj[2].reg(2);
mail obj2;
ifstream filein;
filein.open("email",ios::in||ios::binary);
if(!filein)
{
cout<<"Unable to open file to read\n";
}
else
{
while(!filein.eof())
{
filein.read((char *)&obj2,sizeof(obj2));
cout<<"username "<<obj2.un<<" passwword "<<obj2.pd<<"\n";
}
filein.close();
}
getch();
}
Also please tell me how to put code into stackoverflow. Manually putting 4 spaces after copy pasting is very tiresome
new code after making changes :
#include<fstream.h>
#include<conio.h>
struct mail
{
char un[25]; // user name
char pd[25]; // passsword
void reg(int);
} obj[5];
void mail::reg(int k)
{
int i=k;
clrscr();
cout<<"Enter user name ( enter unique name )\n";
cin>>un;
cout<<"Enter password\n";
cin>>pd;
ofstream filout;
filout.open("email",ios::app|ios::binary);
if(!filout) {
cout<<"cannot open file\n";
} else {
cout<<"\n "<<i;
filout.write((char *)&obj[i],sizeof(mail));
filout.close();
}
cout<<"You are now registered. \n";
getch();
} // end of sign up or register func
int main()
{
int t;
clrscr();
obj[0].reg(0);
obj[1].reg(1);
obj[2].reg(2);
mail obj2;
ifstream filein;
filein.open("email",ios::in|ios::binary);
if(!filein) {
cout<<"Unable to open file to read\n";
} else {
while(filein) {
filein.read((char *)&obj2,sizeof(obj2));
cout<<"username "<<obj2.un<<" passwword "<<obj2.pd<<"\n";
}
filein.close();
}
getch();
}
Im still facing problem. I write 3 object. But iam getting 4 output records. Last one is duplicated.
You have an improper file loop, an EOF() loop is bad practice and often can lead to undefined behavior, a proper loop would be as follows:
filein.read((char *)&obj2,sizeof(obj2));
while(filein)
{
cout<<"username "<<obj2.un<<" passwword "<<obj2.pd<<"\n";
filein.read((char *)&obj2,sizeof(obj2));
}
the structure of this loop allows the file to check the file for EOF before reading again, while the eof loop will read the eof in THEN check, leading to some junk at the end.
your fileIn variable uses improper flags, you use '||' the logical OR
operator instead of the '|' logical bitwise operator. This could be a
possible reason for your error.
you have some issues with your program, void main() make most
people here cringe, main ALWAYS returns int