m0_62038145
2021-09-19 18:31
采纳率: 100%
浏览 137

求看一下啊c++ ifstream读取文件为何失败

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    char ch1;
    ofstream outFile("d:\\vs c++ document\\first-35\\file1.txt",ios::out);
    if(!outFile)
    {cout<<"文件打开错误!";return 0;}
    outFile<<"I am a file.Please read me.";
    cout<<"文件内容I am file.Please read me."<<endl;
    outFile.close();
    ifstream inFile("d:\\vs c++ doucument\\first-35\\file1.txt",ios::in);
    if(!inFile)
    {cout<<"文件打开错误!";goto Exit;}
    streampos pos=inFile.tellg();
    cout<<"当前文件指针位置:"<<pos;
    inFile.get(ch1);  cout<<",字符"<<ch1<<endl;
    pos=inFile.tellg();
    cout<<"读取一个字符后文件指针位置:"<<pos<<endl;
    inFile.seekg(5,ios::beg);
    pos=inFile.tellg();
    cout<<"后移5个字符后文件指针位置:"<<pos;
    inFile.get(ch1);  cout<<",字符"<<ch1<<endl;
    pos=inFile.tellg();
    cout<<"读取一个字符后文件指针位置:"<<endl;
    pos=pos+(streampos)19;
    inFile.seekg(pos);
    cout<<"后移19个字符后文件指针位置:"<<pos;
    inFile.get(ch1);cout<<",字符"<<ch1;
    inFile.get(ch1);cout<<ch1<<endl;
    inFile.close();
    
Exit:
    return 0;
}


程序在ifstream那里读取文件失败。

1条回答 默认 最新

相关推荐 更多相似问题