FStand不是在C++中创建文件
我已经检查了几个这样的问题,比如: 及 但他们的答案都帮不了我。在花了这么多时间调试之后,我无法检测到这个bug。 所以,我在这里再次提出这个问题 我的程序的代码是:FStand不是在C++中创建文件,c++,file-io,fstream,C++,File Io,Fstream,我已经检查了几个这样的问题,比如: 及 但他们的答案都帮不了我。在花了这么多时间调试之后,我无法检测到这个bug。 所以,我在这里再次提出这个问题 我的程序的代码是: #include<iostream> #include<fstream> #include<string.h> using namespace std; int main(){ ofstream file; file.open("data.dat",fstream::out)
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main(){
ofstream file;
file.open("data.dat",fstream::out);
file<<fflush;
if(!file)
cout<<"error"<<strerror(errorno);
file.close();
return 0;
}
这是处理文件处理程序的主要部分。程序的其余部分处理一些数据并将其写入文件,我认为这既不相关也不影响文件处理
#include <iostream>
#include <fstream>
#include <string.h>
#include <errno.h>
int main (int argc, char* argv[])
{
std::ofstream file;
file.open("data.dat", std::fstream::out);
file << "Hello" << std::endl;
if (!file)
std::cout << "error" << strerror(errno);
file.close();
return 0;
}
有趣的是,程序没有闪现任何错误。您的代码通常只需稍加修改即可工作,文件只是在运行程序的当前工作目录中创建的,而不是在可执行文件所在的目录中创建的。不过,您可能还需要解决很多其他问题:
int main(){
ofstream file;
file.open("data.dat") // no need to call for ofstream ->fstream::out
// file<<fflush; fflush won't work since there is nothing unwriten
if(!file.is_open()) // use is_open();
cout<<"error"<<strerror(errorno);
// write something to file
file << " ";
file.close();
return 0;
}
#include <iostream>
#include <fstream>
// if including things from the C standard library in a C++ program,
// use c[header] instead of [header].h; you don't need any here though.
using namespace std;
int main()
{
// no need to call open(), the constructor is overloaded
// to directly open a file so this does the same thing
ofstream file("data.dat");
if(!file)
{
cout << "Couldn't open file" << endl;
return 1;
}
file.close();
// return 0; is not needed, your program will automatically
// do this when there is no return statement
}
有关打开文件失败原因的详细信息,请查看和。当使用C++流处理文件时,检查ErrNO是不需要的。
#include <iostream>
#include <fstream>
#include <string.h>
#include <errno.h>
int main (int argc, char* argv[])
{
std::ofstream file;
file.open("data.dat", std::fstream::out);
file << "Hello" << std::endl;
if (!file)
std::cout << "error" << strerror(errno);
file.close();
return 0;
}
主要区别:
你没有包括在内。
您正在将errorno传递给strerror,而不是errno,这是正确的数字。
这适用于Ubuntu 14.04 64位。我使用没有标志的g++编译
此外,我建议您永远不要使用名称空间std;当您开始与其他库集成时,这会非常恼人,比如Boost Boost的库可以与每个C++标准库的功能重叠。p> 如果文件被打开,您可以在GDB和procfs的帮助下找到它的位置。只需在文件打开但尚未关闭的位置放置一个断点。在调试器上运行程序,直到触发断点。然后使用以下命令:
ls -l /proc/<pid>/fd
其中是程序的PID。文件的完整路径应该在输出中的某个位置。可能创建了文件,但不在您期望的位置?如果从命令行运行程序,则会在当前目录中创建文件。如果您从IDE运行它,那么当前目录将在项目设置中设置。@Gaurav,不能保证iostreams将errno设置为on error。并且该文件是在当前工作目录中创建的,如果运行/bin/ls,则该目录不一定是程序所在的目录。默认情况下,它不会显示/bin的内容!不过,它有一些地方是错误的:不应该将fflush写入该文件。它是C库中的一个函数,用作fflushFILEstruct;。在执行代码时,它只向文件中写入1。此外,您不应该无条件地写入文件。检查是否可以先打开该文件,如果是这样,则写入该文件并仅在这种情况下将其关闭。@Gaurav嗯,卸载并不一定意味着删除配置文件。在任何情况下,如果您想更改此行为,请询问您最喜欢的搜索引擎有关代码::阻止生成路径,而不是重新安装它。fflush和errorno仍然是错误的。请参见上面的注释。这并没有解决实际问题,仍然是可怕的代码。@amchacon是的,它应该是,因为它更易于阅读,所以我首选是打开的,您修复了名称空间的问题,但是您的程序与未包含errno头的原始程序一样损坏。您修复了缺少的errno include,但在全局命名空间中缺少std::fflush或fflush。我知道这是可以编译的,但您依赖的编译器包含一个在std名称空间中定义fflush的头,作为包含的其他头的一部分,这不是一件好事。我还建议您使用-Wall-Wextra-pedantic进行编译,它会告诉您,顺便说一句,std::flush是在中定义的,所以我不明白这是怎么回事?我还喜欢使用int-argc,char*argv[]来提醒自己一个标准实现,这样我就不会开始变得懒惰而只使用main。提醒自己我可以创建命令行应用程序,这也很好,它还可以教育其他人这也是可能的。您从哪里获得定义std::fflush的信息?