【发布时间】:2014-11-30 05:27:08
【问题描述】:
我使用 VS2012 编写了一个小型 VC++ 程序并尝试读取一个文本文件。我将文件放在发布文件夹中。但是,在使用绝对文件目录之前,我无法读取该文件。虽然我在网上找不到有用的信息。代码是这样的
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
cout<<"Hello World!"<<endl;
string test;
ifstream myfile;
myfile.open("E:\\Glus\\Project2\\Release\\bunny.txt");
if(myfile.is_open())
{
string s0,s1;
int x0, x1;
myfile>>s0>>x0;
cout<<s0<<x0<<endl;
myfile>>s1>>x1;
cout<<s1<<x1<<endl;
}
else
{
cout<<"Error in reading file!"<<endl;
}
myfile.close();
cin>>test;
return 0;
}
谢谢!
【问题讨论】:
-
你告诉VS启动你的程序的目录是什么?这不一定与
.exe文件所在的目录相同。 -
默认情况下 VS 使用项目文件夹作为当前目录,而可执行文件构建到
..\Win32\Release等。
标签: c++ io ifstream absolute-path