此代码应显示" abc123"但相反,会显示一些不可识别的字母
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main()
{
fstream fin("rabbit.txt",ios::in|ios::out);
fin.seekg(0);
char arr[100]= "abc123 abc abc ejgrjgn ejrog rge k abc";
char a[100];
fin<<arr<<'\n';
fin.seekg(0);
char ch;
fin.get(ch);
fin.seekg(0);
fin>>a;
cout<< a;
}
答案 0 :(得分:0)
您应该始终检查错误:
fstream fin("rabbit.txt",ios::in|ios::out);
if ( fin.is_open() )
{
fin.seekg(0);
char arr[100]= "abc123 abc abc ejgrjgn ejrog rge k abc";
char a[100];
fin<<arr<<'\n';
}
else
{
cout<<"failed to open fstream\n";
}
您需要ios :: app或ios :: trunc来创建和打开文件。