在 C++ Visual Studio 2010 中创建输出文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13515114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating output file in C++ Visual Studio 2010
提问by user1845360
I have a C++ project which I initially compiled with g++. Now, I am trying to make it work in Visual Studio 2010 and the problem is that the regular commands that create an empty output file for writing:
我有一个最初用 g++ 编译的 C++ 项目。现在,我试图让它在 Visual Studio 2010 中工作,问题是创建空输出文件的常规命令用于写入:
#include <fstream>
using namespace std;
ofstream fout("afile.txt");
fout<<"write someting"<<endl;
fout.close();
will not work in VS2010 (no file is created). Even if the file already exists nothing is written on it.
不会在 VS2010 中工作(没有创建文件)。即使文件已经存在,也不会写入任何内容。
So, any ideas what is the correct way of opening a file for writing from C++ in VS2010 ?
那么,任何想法在 VS2010 中打开用于从 C++ 写入的文件的正确方法是什么?
回答by Caribou
My suspicion is that the file is created somewhere other than you expected - Check out where the working directory is pointing. This is 2012, but it is very similar if not the same... Right click on the project in vs2010, and select properties, Then select debugging and look at the working directory:
我怀疑该文件是在您预期之外的其他地方创建的 - 检查工作目录指向的位置。这是2012年的,不过不一样就很像了……在vs2010中的项目上右键,选择properties,然后选择debugging查看工作目录:
Alternitively, build it, and copy the app to a directory you create - Then run it on the command line to check whether its a permissions problem.
或者,构建它,并将应用程序复制到您创建的目录 - 然后在命令行上运行它以检查它是否存在权限问题。
回答by MartenBE
Following code works fine by me:
以下代码对我来说很好用:
#include <fstream>
using namespace std;
int main(){
ofstream fout("afile.txt");
fout << "write someting"<<endl;
fout.close();
return 0;
}
You didn't use the main method (strange that it didn't give any syntax errors). Make sure when you creat a project like this it is a ConsoleApplication and it's an empty project (you can check this option under "Additional Options" in the Win32 Application Wizard (The wizard used to create a project).
您没有使用 main 方法(奇怪的是它没有给出任何语法错误)。确保当您创建这样的项目时,它是一个 ConsoleApplication 并且它是一个空项目(您可以在 Win32 应用程序向导(用于创建项目的向导)中的“附加选项”下选中此选项。
Standard C++ should work in Visual Studio. If you want to be sure you're using only standard C++ code, you can always use the /Za compiler option (See http://msdn.microsoft.com/en-us/library/0k0w269d.aspx)
标准 C++ 应该在 Visual Studio 中工作。如果您想确保只使用标准 C++ 代码,您可以随时使用 /Za 编译器选项(请参阅http://msdn.microsoft.com/en-us/library/0k0w269d.aspx)
回答by user6408370
manually add afile.txt into the same location with your source file, and then run the your program. text will be written into the file. But make sure afile.txt file is not open when you run the program
手动将 afile.txt 添加到与源文件相同的位置,然后运行您的程序。文本将写入文件。但请确保运行程序时afile.txt 文件未打开
回答by MikhailJacques
I had the same problem which puzzled me for a few minutes. Examine your project directory. I bet you will find your file in there. If not, then you need to specify a working directory for either debug or release modes. In MS Visual Studio you can do so by clicking the Properties on the Project menu and then specifying in the Configuration properties in lets say a Debugging tab in the Working directory field the directory from which you want your project to be launched.
我遇到了同样的问题,让我困惑了几分钟。检查您的项目目录。我打赌你会在那里找到你的文件。如果没有,那么您需要为调试或发布模式指定一个工作目录。在 MS Visual Studio 中,您可以通过单击“项目”菜单上的“属性”,然后在“配置”属性中指定工作目录字段中的“调试”选项卡来执行此操作,即您希望从中启动项目的目录。