'Access violation' using fstream

1
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
char r;
fstream file1("text.txt", ios::in |ios::binary);
fstream file2("text.txt", ios::out | ios::binary);
r='r';
for(int i=0; i<100; i++)
    file2.write((char*)r, sizeof(char));
while(!file1.eof()) 
{
    file1.read((char*)r, sizeof(char));
    cout<<r<<"\n";
}
file1.close();
file2.close();
getch();
}

when I run this in vc++ 2010, I get the following error during run time:

Unhandled exception at 0x55361f68 (msvcp100d.dll) in file io.exe: 0xC0000005: Access       violation reading location 0x00000072.

what could be causing this error? this happens while reading the line :

file2.write((char*)r, sizeof(char));

did I make any mistake? If yes please point it out for me (thanks in advance).

Update: I am still not getting my expected output (after correcting (char*)r to (char*)&r). the output that I get is just: r. shouldn't I expect 100 characters to be displayed starting from 'r'? If not, please tell me why and thanks in advance.

c++
fstream
visual-c++-2010
asked on Stack Overflow May 15, 2014 by (unknown user) • edited May 15, 2014 by (unknown user)

3 Answers

3

You need

 file1.read((char*)&r, sizeof(char));

or

 file1.read(&r, sizeof(char));
answered on Stack Overflow May 15, 2014 by user3344003 • edited May 15, 2014 by user3344003
0

In addition to the other answer, there's also another problem your code has.

Streams perform buffered I/O by default; when writing into file1, the contents that you've written probably haven't been outputted to the actual file yet. The contents are actually stored in a temporary buffer for efficiency. Writing to the actual file is an operation reserved for an explicit flush(), when close() is called, or when the file stream goes out of scope and is destructed.

The problem in your code is that directly after writing to the file stream, you perform input without determining whether that output data was written to the actual file. This can cause Undefined Behavior if you assume that the data was read successfully from the input file to the variable.

File streams that depend on each other should be synchronized. Namely, when a file stream is trying to read from the same file that you have written to, then the output file stream must be flushed. This can be facilitated by "tying" the streams together, this is done using tie():

file1.tie(&file2);

When file1 performs input, file2 will then be flushed, forcing the data in its buffer to be written the file.

Another problem you have is that you don't check if the file streams were constructed correctly, or that you have successfully read from file1. You can use if() statements to do this:

std::fstream file1("text.txt", std::ios_base::in  | std::ios_base::binary);
std::fstream file2("text.txt", std::ios_base::out | std::ios_base::binary);
char r('r');

if (file1 && file2)
{
    file1.tie(&file2);

    for (int i = 0; i < 100; ++i)
        file2.write(&r, sizeof(char));

    while (file1.read(&r, sizeof(char))) {
        std::cout << r << std::endl;
    }
}
answered on Stack Overflow May 15, 2014 by 0x499602D2
-1

You started reading from a file immediately after writing on that file and without closing the write file stream. Until you close the write file stream it will not commit the writings. So there is a change of getting access violation as it holds the control.

Try following code

#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
char *r="r";

fstream file2("text.txt", ios::out | ios::binary);

for(int i=0; i<100; i++)
    file2.write((char*)r, sizeof(char));
file2.close();

fstream file1("text.txt", ios::in |ios::binary);

while(!file1.eof()) 
{
    char rr;
    file1.read(&rr, sizeof(char));
    cout<<rr<<"\n";
}

file1.close();
getch();
}

You have tried to cast a single char to char * and also tried to read using fread without passing r's address. That's why the problem is occurring. Please carefully see my code above, it will fix your issues.

answered on Stack Overflow May 15, 2014 by MD. Nazmul Kibria • edited May 15, 2014 by MD. Nazmul Kibria

User contributions licensed under CC BY-SA 3.0