如何解决c++文件读写需要同步吗?在窗口操作系统?
我正在制作简单的隐写术,在一个字节的每一端隐藏每一位并找到。
fstream file; // readfile
file.open(image.bmp,ios::out | ios::in | ios::binary)
stego.seekg(first_location,ios::beg);
string cryptogram = "";
int i = 0;
char letter = 0;
char colorBit[8];
int temp;
while (!stego.eof()) {
stego.read(colorBit,8); //read 8byte
loc += 8;
stego.seekg(loc,ios::beg);
for (int i = 7; i >=0; i--) {
//get last bit for each byte those become letter
temp = colorBit[i] & 1;
letter = letter << 1;
letter = letter | temp;
}
//cryptogram += letter;
cout << letter;
if (letter == 13) { // if letter is enter stop
cout << stego.tellg() << endl;
break;
}
letter = 0;
}
cout << cryptogram << endl;
stego.close();
但在过程中出现错误。 就像如果程序读取 5000 偏移量并获得一个字符,之后它必须去 5008 偏移量但去 -1 偏移量所以我做了 while 循环来修复,如下所示
while(file.tellg() != expected_offset){
file.seekg(expected_offset,ios::beg);
}
但是没有用。所以我使用了 clear()、sync() 函数,它终于奏效了。 关键是当我在 mac os 中制作相同的程序时没有问题 在窗口中,如果我插入短字符串(可能是 300~400 个字母)也没有问题。 但如果长度更大,错误就会出现。
我不明白为什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。