Segmentation fault while reading a binary file

Hi..
I am creating a program that writes to a binary file and then reads from it. It also checks that if the file exists then append the file or else write a new file. But after writing the same file again and again as it crosses like 24 bytes it gives a segmentation error . In addition to that i m not able to read the file cout<<"Read:<<*readData<<endl;; gives me only first char.

Please help me out here.....

here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>

using namespace std;

ifstream::pos_type size;
class BinaryIO
{
        private:
                char* readData;
                char* writeData;

        public:
                void readFile(string);
                void writeFile();
};

void BinaryIO :: readFile(string sPath)
{
        ifstream file (sPath.c_str(), ios::in|ios::binary|ios::ate);
        if(file.is_open())
        {
                size = file.tellg();
                cout<<"Size "<<size;
                readData = new char[size];
                file.seekg(0,ios::beg);
                file.read((char*)&readData,size);
                cout<<"Read:"<<*readData<<endl;
                file.close();
                //delete readData;
        }
}

void BinaryIO :: writeFile()
{
        struct stat buffer;
        ofstream file;
        int status;
        status = stat("output.bin",&buffer);
        cout<<"Status : "<<status<<endl;
        if(stat("output.bin",&buffer) == 0)
        {
                cout<<"in first if"<<endl;
                file.open("output.bin", ios::out|ios::binary|ios::app|ios::ate);
                size = file.tellp();
                //writeBuffer = new char[size];
                writeData = "My name is Herat Acharya";
                file.write((char*)&writeData,sizeof(writeData));
        }
        else
        {
                cout<<"in second if"<<endl;
                file.open("output.bin", ios::out|ios::binary);
                size = file.tellp();
                //writeBuffer = new char[size];
                writeData = "I live in Buffalo";
                file.write((char*)&writeData,sizeof(writeData));
        }
        file.close();
        //delete [] writeBuffer;
}
int main()
{
        BinaryIO bin;
        bin.writeFile();
        bin.readFile("output.bin");
        return 0;
}


here is the output of the code
1
2
3
4
Status : 0
in first if
Size 48Read:I
Segmentation fault


Thanks in advance..
Herat
Last edited on
1
2
file.read((char*)&readData,size);
cout<<"Read:"<<*readData<<endl;


why are u passig char ** to the file.read() method.
U must do
 
file.read((char*)readData,size);


And when u are printing *readData u are printing the first character only because readData is a pointer to a char and by doing *readData u are accessing the character ointed to by that address. Remeber how a string is represented. Char * will point to the first element of the string.
U have to do
 
cout<<"Read:"<<readData<<endl;



Last edited on
1
2
file.read((char*)&readData,size);
cout<<"Read:"<<*readData<<endl;

should be
1
2
file.read(readData,size);
cout<<"Read:"<<*readData<<endl;


1
2
writeData = "My name is Herat Acharya";
file.write((char*)&writeData,sizeof(writeData));

should be
1
2
writeData = "My name is Herat Acharya";
file.write(writeData,strlen(writeData));

You have that part twice.
@dkalita and @kbw.. I tried that .. that was the first thing i made but the only problem is that the output.bin is in human readable format, shouldnt it be in a binary format.Then i stumbeled upon something called c+ serialization... but i m getting a seg fault in that. Foreg: my string is I live in Buffalo. . It shows me the string I live in buffalo .. it should be in binary format isnt it?

Currently with my program i m getting an output

¯^W@^@^@^@^@^@<80>^V@^@^@^@^@^@^@<89>^W@^@^@^@^@^@<80>^V

and so on...


Any comments on that
Thanks in advance.
Topic archived. No new replies allowed.