[C ++] ofstream保存在哪里? [英] [C++] where does ofstream save?

查看:198
本文介绍了[C ++] ofstream保存在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以收集用户输入并将其输出到特定格式的文件中。我似乎无法弄清楚输出到哪里。我正在使用Code :: Blocks with g ++。



我尝试过:



我查看了整个文件,但找不到输出文件。

I have a program that gathers user input and outputs it to a file in a specific format. I can't seem to figure out where it outputs to. I'm using Code::Blocks with g++.

What I have tried:

I have looked all through the files, but cannot find the output file.

推荐答案

打开文件流时,传递位置该对象的文件(包括名称)。通常,仅传递文件的名称(例如, _ofstream.open(file.txt)),该文件在同一目录中创建该文件,其中程序为居住。在大多数情况下,您需要传递完整路径。例如,如果以下是您的计划,

When you open a stream to a file, you pass the location of the file (including the name) to that object. Typically, a name of the file is passed only (like, _ofstream.open("file.txt")) which creates the file in the same directory, where the program is residing. In most cases, you would require to pass a full path. So for example if following is your program,
#include <iostream>
#include <fstream>

using namespace std;

int main() {
   ofstream _filestream;
   _filestream.open("file.txt");

   _filestream << "Hello world!" << endl;
}



然后你会在同一目录中得到一个新文件 created (因为路径不符合目录它必须去哪里等)。否则,您可能需要传递完整路径。这将是,



1. Windows上的C:\ MyData \ .File.txt - 提供了足够的权限。

2. Linux环境中的/home/afzaal/Documents/file.txt - 可能要求权限



阅读代码。那段代码有答案。有关更多参考,请阅读: ofstream - C ++ Reference [ ^ ], ofstream :: open - C ++参考 [ ^ ]。 Open是负责打开写入数据的流的函数。


Then you will get a new file created, inside the same directory (because the path is not qualified with the directory where it must go etc.). Otherwise, you might require to pass full path. Which would be,

1. "C:\MyData\File.txt" on Windows -- provided enough permissions are granted.
2. "/home/afzaal/Documents/file.txt" on Linux environments -- permissions may be asked.

Read the code. That code has the answer. For more references, please read: ofstream - C++ Reference[^], ofstream::open - C++ Reference[^]. Open is the function responsible for opening the streams where the data is written at.


这篇关于[C ++] ofstream保存在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆