简体   繁体   English

使用fstream的“访问冲突”

[英]'Access violation' using fstream

#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: 当我在vc ++ 2010中运行此程序时,在运行时出现以下错误:

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)); 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). 更新:我仍然没有得到我的预期输出(将(char *)r校正为(char *)&r之后)。 the output that I get is just: r. 我得到的输出是:r。 shouldn't I expect 100 characters to be displayed starting from 'r'? 我不应该期望从'r'开始显示100个字符吗? If not, please tell me why and thanks in advance. 如果没有,请提前告诉我原因并谢谢。

You need 你需要

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

or 要么

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

In addition to the other answer, there's also another problem your code has. 除了其他答案之外,您的代码还存在另一个问题。

Streams perform buffered I/O by default; 流默认情况下执行缓冲的I / O; when writing into file1 , the contents that you've written probably haven't been outputted to the actual file yet. 当写入file1 ,您写入的内容可能尚未输出到实际文件中。 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. 写入实际文件是为显式flush() ,调用close()或文件流超出范围并被破坏时保留的操作。

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() : 可以通过将流“绑定”在一起来简化此过程,这可以使用tie()

file1.tie(&file2);

When file1 performs input, file2 will then be flushed, forcing the data in its buffer to be written the file. file1执行输入时,将刷新file2 ,强制其缓冲区中的数据写入文件。

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 . 您遇到的另一个问题是,您没有检查文件流的构造是否正确,或者您是否已成功从file1读取file1 You can use if() statements to do this: 您可以使用if()语句执行此操作:

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;
    }
}

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. 您尝试将单个char强制转换为char * ,还尝试使用fread进行读取而不传递r的地址。 That's why the problem is occurring. 这就是问题发生的原因。 Please carefully see my code above, it will fix your issues. 请仔细查看上面的代码,它将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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