读取和写入不能同时工作
Read and Writing not working simultaneously
在这个程序中,我想转到文件中的特定位置并在那里读取。
如果它是空格,我将在那里写入缓冲区,否则我想搜索下一个"空白空间"。现在问题出在我写在评论put 2 lines under comment from here
中的那些行上。如果我包含这些行,我的输出文件是空白的。如果我删除这 2 行,它会在文件中正确写入。但我想在写作之前阅读。
通过这 2 行代码,我可以阅读。所以你能建议我任何替代的读取方式,以便缓冲区文件在读取后进入输出文件而不是保持空白。
或者我在这里做错了什么?
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//moving the seekp pointer of the fstream
void seek_key(fstream &fout,int k){
if(k==0)
fout.seekp(0,ios::beg);
else{
k=((k*2)-1)+(k*42);
fout.seekp(k,ios::beg);
}
}
//moving the seekg pointer of fstream
void seec_key(fstream &fout,int k){
if(k==0)
fout.seekg(0,ios::beg);
else{
k=((k*2)-1)+(k*42);
fout.seekg(k,ios::beg);
}
}
//to put n spaces in the file so that later i can put record in that location
//actually doing hashing files
void make_file(fstream &fout,int n){
int i;
i=n;
i--;
while(i>0){
for(int j=0;j<42;j++)
fout<<" ";
fout<<"n";
i--;
}
}
struct student{
string roll;
string name;
string cgpa;
};
class buffer{
public:
string buf;
void pack(student s);
void unpack(istream fin,student s);
};
void buffer::pack(student s){
buf=s.roll;
buf=buf+"|";
buf=buf+s.name;
buf=buf+"|";
buf=buf+s.cgpa;
buf=buf+"|";
}
//cin overloading to get input into student structure
istream &operator >> (istream &in,student &s){
cout<<"enter student name: ";
in>>s.name;
cout<<"enter student roll: ";
in>>s.roll;
cout<<"enter cpga: ";
in>>s.cgpa;
}
//for adding student buffer into the file
void add(fstream &fout,buffer &b,student &s,int k){
int key=atoi(s.roll.c_str());
int v=key%k;
char test;
seek_key(fout,v);
seec_key(fout,v);
// put 2 lines under comments from here
fout>>test;
cout<<"this is test."<<test<<".test"<<endl;
fout<<b.buf;
}
int main(){
student s;
buffer b;
fstream fout;
fout.open("hash.txt");
int n;
cout<<"enter the no. of records: ";
cin>>n;
make_file(fout,n);
char ans;
do{
cin>>s;
b.pack(s);
add(fout,b,s,n);
cout<<"to enter more students press y else n";
cin>>ans;
}while(ans=='y'||ans=='Y');
fout.close();
return 0;
}
基本上,你有以下代码:
fout>>test; // line 1
// code not using fout removed
fout<<b.buf; // line 2
基本上,这意味着您在没有干预搜索的情况下阅读和写作:这是未定义的行为!文件流的状态可以由状态机描述:
initial -> unbound
unbound + write -> write mode
write mode + write -> write mode
write mode + seek -> unbound
write mode + read -> undefined behavior
unbound + read -> read mode
read mode + read -> read mode
read mode + seek -> unbound
read mode + write -> undefined behavior
在写入模式下读取或以读取模式写入是未定义的行为有几个原因:
- 性能:需要适当设置内部缓冲区,这是一项不平凡的操作,检查正确的缓冲区会不必要地减慢操作速度。
- 用户期望的适当流位置完全不清楚。最好让用户澄清期望。
- 使用非平凡转换时,可能需要在写入模式下写入移位序列的结尾。这不应该由隐式操作触发,即用户应该更好地意识到(当然,我怀疑许多用户是否意识到搜索操作可能会写入移位序列的结尾)。
无论如何:寻找您实际想要写入数据的适当位置应该可以解决问题。
相关文章:
- 为什么 HeapFree() 不能正常工作?
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- 为什么"std::is_function_v"不能按预期工作?
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- C++自定义删除运算符不能正常工作?
- 为什么使用 exec() 重新启动程序不能正常工作?
- C++:返回本地对象,但不能正常工作
- 有没有办法关闭文件? fclose 不能很好地工作
- 为什么我不能声明一个 constexpr 本地,而一个 const 一个工作?
- ostream_iterator运算符=在pair<int,int>上失败,但在包装器类上工作。我不能为成对<>重载运算符<<吗?
- 为什么带lcov的codecov在Travis上不能正常工作,而在我当地的Linux Mint上却不能正常工作
- 为什么sizeof函数在这里不能正常工作
- 为什么 decltype(declval()<T>.func()) 可以工作,而 decltype(&T::func) 不能工作?
- 静态 constexpr 函数在模板结构中工作,但不能在结构中工作.为什么?
- 为什么这个base64解码/编码功能不能正常工作?
- 为什么带有 const 关键字的构造函数可以工作,而没有它就不能工作?
- 如果 (QString.contains()) 不能正常工作,请使用 temp bool 变量进行更正
- 你不能在 void 指针上使用指针算法,那么 void 指针数组是如何工作的呢?