简体   繁体   中英

Fwrite and Fread for Vector return Segmentation Fault

I made codes in c++, for encryption and decryption. first code creates an output in vector and then write it in a file by using fwrite, and the second reads that output from the first by using fread. Here is the snippet of my codes :

1st code :

.....
string a;
vector<long long int> c;

cout << "message to be encrypted = ";
cin >> a;   
cout << endl;

cout << "Encrypted message : ";
for (i=0;i<a.size();i++) 
{
    x=(int)a.at(i);
    cout << x << " ";
    c.push_back(powerMod(x,e,n));
}

for (i=0;i<c.size();i++) 
{
    //cout << char(c.at(i));
}
cout << endl;

//Write ciphertext c to a file
FILE * pWrite;
pWrite = fopen ("ciphertext", "w");
fwrite (&c , sizeof(c), 1, pWrite);
fclose (pWrite);

The output is :

message to be encrypted = test
Encrypted message : 116 101 115 116 

And then the 2nd code :

....
//Read Ciphertext from ciphertext
FILE * pRead2;
pRead2 = fopen ("ciphertext", "r");
fread (&c , sizeof(c), 1, pRead2);
//cout << "ciphertext is " << c << endl;

// Decryption
cout << "Decrypted message : ";
for (i=0;i<c.size();i++) 
{
    cout << powerMod(c.at(i),d,n) << " " ;
}
cout << endl;

But it return :

Segmentation Fault(Core Dumped)

I appreciate any help, since I don't know where is the problem, in the fwrite or in the fread. But I think the problem is in the 2nd, when it tries to read the ciphertext (which is a vector), because if I erase that lines, the program is running perfectly, but without decrypting the message.

Thanks.

It's because you write a pointer to the vector object instance , and not the actual vector data. Use

fwrite (&c[0], sizeof(vector<long long int>::value_type), c.size(), pWrite);

Also remember that sizeof(c) returns the size of the vector object instance , not the number of items in the vector.

You have a similar problem when reading the vector. You have to do it one by one in a loop, pushing the items on to the vector again.


With C++ there are simpler ways of doing this, if you learn to use the C++ I/O stream library and some nice standard algorithms and use iterators .

To write a vector to a file:

std::ofstream os{"ciphertext", std::ios::out};

std::copy(std::begin(c), std::end(c),
          std::ostream_iterator<long long int>(os));

And to read from the file:

std::ifstream is{"ciphertext", std::ios::in};

std::copy(std::istream_iterator<long long int>(is),
          std::istream_iterator<long long int>(),
          std::back_inserter(c));

There's actually an even simpler way to read from a file into a vector:

std::ifstream is{"ciphertext", std::ios::in};

std::vector<long long int> c(std::istream_iterator<long long int>(is),
                             std::istream_iterator<long long int>());

This relies on the std::vector constructor taking two iterators as arguments.


If you don't want to use text files, but binary files, you unfortunately have to loop manually and write/read the data, ie you manually have to do what std::copy does for you.

Something like this to write the data:

std::ofstream os{"ciphertext", std::ios::out | std::ios::binary};

for (const auto& value : c)
    os.write(reinterpret_cast<const char*>(&value), sizeof(value));

And like this to read it:

std::ifstream is{"ciphertext", std::ios::in | std::ios::binary};

long long int value:
while (is.read(reinterpret_cast<char*>(&value), sizeof(value)))
    c.push_back(value);

If you don't have the C++11 range-based for loop (used in the writing example above), use a normal classic iteration for loop:

std::vector<long long int>::const_iterator i;
for (i = c.begin(); i != c.end(); ++i)
    os.write(reinterpret_cast<const char*>(&(*i)), sizeof(*i));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM