我试图用ifstream打开一个文件,我想用一个字符串作为路径(我的程序创建一个字符串路径).它会编译,但它保持空白.
string path = NameOfTheFile; // it would be something close to "c:\file\textfile.txt"
string line;
ifstream myfile (path); // will compile but wont do anything.
// ifstream myfile ("c:\\file\\textfile.txt"); // This works but i can't change it
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的是Windows 7,我的编译器是VC++ 2010.
我只是想使用copyfile来复制文件,它就像那样简单,但它不会工作.我用Google搜索并查看了20个链接,他们都说" object.CopyFile(source,destination [,overwrite])"
问题是我无法让它为我复制txt文件,我已经尝试将其作为管理员运行但仍然无法正常工作.另外我需要将源和目标设置为lpctstr(因为它不会使用多字节字符编译而我的其他代码将无法工作,除非我使用Unicode字符集).
我的代码是
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
CopyFile("C:\\Somefolder\\file.txt","C:\\folder\\file.txt",0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在运行Windows 7,vc ++ 2010,编译为调试,抱歉,如果我错过任何东西.