我读过那个<fstream>早期的<exception>.忽略了例外fstream情况不是很有用的事实,我有以下问题:
可以使用该exceptions()方法在文件流上启用异常.
ifstream stream;
stream.exceptions(ifstream::failbit | ifstream::badbit);
stream.open(filename.c_str(), ios::binary);
Run Code Online (Sandbox Code Playgroud)
任何尝试打开不存在的文件,没有正确权限的文件或任何其他I/O问题都将导致异常.使用自信的编程风格非常好.该文件应该在那里并且可读.如果条件不满足,我们会得到例外.如果我不确定文件是否可以安全打开,我可以使用其他功能来测试它.
但现在假设我尝试读取缓冲区,如下所示:
char buffer[10];
stream.read(buffer, sizeof(buffer));
Run Code Online (Sandbox Code Playgroud)
如果流在填充缓冲区之前检测到文件结束,则流决定设置failbit,如果启用了异常,则会触发异常.为什么?这有什么意义?我本可以eof()在阅读后验证只是测试:
char buffer[10];
stream.read(buffer, sizeof(buffer));
if (stream.eof()) // or stream.gcount() != sizeof(buffer)
// handle eof myself
Run Code Online (Sandbox Code Playgroud)
这种设计选择使我无法在流上使用标准异常,并迫使我在权限或I/O错误上创建自己的异常处理.或者我错过了什么?有什么出路吗?例如,我可以轻松测试是否可以sizeof(buffer)在流之前读取字节数吗?
看看之前的答案,似乎虽然std::ostream不是可移动的,但std::ofstream应该是.但是,这段代码
#include <fstream>
int main()
{
std::ofstream ofs;
std::ofstream ofs2{std::move(ofs)};
}
Run Code Online (Sandbox Code Playgroud)
似乎没有在我试过的任何版本的gcc或clang中编译(使用--std = c ++ 11或--std = c ++ 14).编译器错误有所不同,但这是我得到的gcc 4.9.0
6 : error: use of deleted function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream&)'
Run Code Online (Sandbox Code Playgroud)
根据标准,这是预期的行为吗?
请注意,之前问过一个非常类似的问题(std :: ofstream是否可以移动?)但是从那时起标准似乎发生了变化(详见 为什么不能移动std :: ostream?)使这些答案过时.当然,这些答案都没有解释为什么上面的代码不能编译.
在尝试使用容器时遇到了这个问题ofstream,因为上述情况不起作用.
我只想写(追加)到日志文件.我在这里查了一下:http:
//www.cplusplus.com/reference/iostream/fstream/open/
所以这就是我所做的
#include <fstream>
fstream outfile;
//outfile.open("/tmp/debug.txt" ); // works, simply for writing
outfile.open("/tmp/debug.txt", fstream::app ); // does nothing
outfile << "START" << endl;
outfile.close();
Run Code Online (Sandbox Code Playgroud) Visual Studio引发此奇怪错误:
不允许不完整的类型
当我尝试创建一个std :: ofstream对象时.这是我在函数中编写的代码.
void OutPutLog()
{
std::ofstream outFile("Log.txt");
}
Run Code Online (Sandbox Code Playgroud)
每当遇到此Visual Studio时抛出该错误.为什么会发生这种情况?
我试图找出打开文件之间的区别,如:
fstream *fileName*("FILE.dat",ios::binary);
Run Code Online (Sandbox Code Playgroud)
要么
fstream *fileName*("FILE.dat",ios::out);
Run Code Online (Sandbox Code Playgroud)
要么
fstream *fileName*("FILE.dat",ios::binary | ios::out);
Run Code Online (Sandbox Code Playgroud)
我发现,所有这些形式是相同的:在所有情况下,即使用产生该文件上的相同的输出*fileName*<<或*fileName*.write().
在我的C++代码中,我想从文本文件(*.txt)中读取并标记每个条目.更具体地说,我希望能够从文件中读取单个单词,例如"格式","堆栈","杰森","欧洲" 等.
我选择用来fstream执行这个任务,我不知道如何设置它的分隔符给我想要使用的(空格,\n以及连字符,甚至是"麦当劳"中的撇号).我想空间并且\n是默认分隔符,但连字符不是,但我想把它们当作分隔符,这样在解析文件时,我会把"blah blah xxx animal - cat"中的单词简单地称为"blah"," blah","xxx","animal","cat".
也就是说,我希望能够从"堆栈溢出","你是" 等获得两个字符串,并且仍然能够同时维护\n和空格作为分隔符.
感谢Mats Petersson关于如何将矢量复制到数组的解释,这看起来很有用.这是代码snipet:
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;
class Student
{
private:
char m_name[30];
int m_score;
public:
Student()
{
}
Student(const Student& copy)
{
m_score = copy.m_score; //wonder why i can use this statment as
strncpy(m_name, copy.m_name, 30); //declare it private
}
Student(const char name[], const int &score)
:m_score(score)
{
strncpy(m_name, name, 30);
}
void print() const
{
cout.setf(ios::left);
cout.width(20);
cout << m_name << " " << m_score << endl;
}
};
int main()
{ …Run Code Online (Sandbox Code Playgroud) 我正在使用这个简单的示例代码运行程序来输出文本文件.我正在使用Xcode,只是从命令行工具启动一个新的C++项目.由于某种原因,程序不会将任何文件输出到我的Mac上.请帮忙弄清楚为什么XCode不会将任何文件输出到我的电脑?谢谢!
#include <iostream>
#include <fstream>
using namespace std;
int main() {
double myNumber = 42.5;
fstream outfile("test.txt", fstream::out);
outfile << "The answer is almost " << myNumber << endl;
outfile.close();
}
Run Code Online (Sandbox Code Playgroud) 如果我执行以下操作:
ifstream file;
file.open("somefile", ios::binary);
unsigned int data;
file >> data;
Run Code Online (Sandbox Code Playgroud)
我的流将始终设置,failbit并且data将保持未初始化.但是,如果我读了一个char或者unsigned char相反,那么流就可以了. perror()告诉我"结果太大".
我在Google上看到的唯一一件事是建议说不operator>>应该用于二进制数据(首选read()),但我发现操作员更清洁,更容易使用 - 并且它不需要投射所有内容.
有人可以解释这个问题吗?