我需要为输出文件的格式创建新的标志.我上课了
class foo{
bar* members;
ofstream& operator<<(ofstream&);
ifstream& operator>>(ifstream&);
};
Run Code Online (Sandbox Code Playgroud)
我想用它像:
fstream os('filename.xml');
foo f;
os << xml << f;
os.close();
Run Code Online (Sandbox Code Playgroud)
这将保存一个xml文件.
fstream os('filename.json');
foo f;
os << json << f;
os.close();
Run Code Online (Sandbox Code Playgroud)
这是一个json文件.
我怎样才能做到这一点?
std::getline当它获得一个时抛出异常eof.这就是我的表现.
std::ifstream stream;
stream.exceptions(std::ifstream::failbit|std::ifstream::badbit);
try{
stream.open(_file.c_str(), std::ios_base::in);
}catch(std::ifstream::failure e){
std::cout << "Failed to open file " << _file.c_str() << " for reading" << std::endl;
}
while(!stream.eof()){
std::string buffer = "";
std::getline(stream, buffer);
//process buffer
//I do also need to maintain state while parsing
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码getline中抛出异常,因为它获得了eof
如何处理这种情况?
std::string buffer = "";
while(std::getline(stream, buffer)){
//also causes getline to hit eof and throw
}
Run Code Online (Sandbox Code Playgroud) 这个问题:如何保护日志免受应用程序崩溃?引导我到另一个 - std::ofstream::close()实际上做了什么?我知道它的召唤flush(),这是一回事.但还有什么?实际关闭文件的是什么?
编辑:让我重新解释一下我的问题 - 在调用过程中对实际文件做了什么实际操作,close()还是只是std::ofstream内部清理?
我刚开始想知道 - 如何实际std::fstream打开这两个std::ios::in并且std::ios::out实际上应该工作?它该怎么办?写一些东西(例如)一个空文件,然后读......什么?刚写的价值?文件"指针"/"光标"在哪里?也许答案已经在那里,但我无法找到它.
我正在编写一个读取大文件(3x280 GB)的程序,并对文件中的数据进行拟合处理.并行化这样的程序非常方便,这可以通过OpenMP轻松完成.
我不明白的是在OpenMP中如何获取私有变量.众所周知,fstream的obejcts是一个不可复制的,并且不可思议地阻止我将它用作私有对象.所以该文件的读者是共享的.
我后来遇到了一些问题,我想把fstreams作为私有,...并猜猜是什么?有效!!!怎么可能这样?!如果对象是不可复制的,那么OpenMP如何为每个内核使用同一对象的不同副本?
这是我的程序的样子:
fstream dataReaderX(Dirs[0].c_str(), ios::in | ios::binary);
fstream dataReaderY(Dirs[1].c_str(), ios::in | ios::binary);
fstream dataReaderZ(Dirs[2].c_str(), ios::in | ios::binary);
#pragma omp parallel num_threads(cpus_num) shared(...) private(...,dataReaderX,dataReaderY,dataReaderZ)
{
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试创建一个程序,询问用户想要读取的文件,当我尝试myfile.open(fileName)得到错误时:" std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'该行没有匹配函数".
string filename;
cout<<"Enter name of file: ";
cin>>filename;
ifstream myFile;
myFile.open(filename); //where the error occurs.
myFile.close();
Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码,我必须在控制台和文件上打印相同的数据.有没有办法填充公共输出流对象,然后使用cout将其显示在控制台上,并使用fstream和iostream库将其导出到文件?
C++的标准库(或linux sys/stat.h,sys/types.h,sys/....库)中是否有一种方法可以在创建文件时使用ofstream(或使用其他文件)设置文件的文件权限库)?
当我创建一个文件时,它只是创建了一些默认的文件权限(我假设当前umask是什么),但我想明确地将权限设置为默认值以外的东西(例如600),我不能只是在启动程序之前设置umask(b/c其他人将运行它).
// Example of creating a file by writing to it
ofstream fp(filename.c_str())
/* write something to it */
Run Code Online (Sandbox Code Playgroud)
有没有办法在C++中执行此操作,如果没有,是否umask可以在C++程序中设置?
例如,在C的标准库中,您可以这样做:
open(filename, O_RDWR|O_CREAT, 0666)
Run Code Online (Sandbox Code Playgroud)
但我不想使用C函数,因为能够使用与fstream对象相关的函数会很好.
(旁注:有一个问题,其标题正是我所寻找的,但事实证明它是无关的.)
我打开了文件的读写模式
使用以下语句
file.open(fileName, ios::in | ios::out | ios::trunc);
Run Code Online (Sandbox Code Playgroud)
在两种模式下打开文件的主要目的是同时读取和写入文件。
但是在我的代码场景中
当我在写入文件后读取文件时,输出显示空白,这表示未保存我的写入内容,因为我没有关闭文件。
我想在完成读写操作后关闭文件
我在Stack Overflow中找到了解决方案,
使用flush()函数保存文件而不关闭
file.flush();
Run Code Online (Sandbox Code Playgroud)
但是,问题是它不适用于我的情况
那么,如何在不关闭的情况下保存c ++ fstream文件?
这是我的完整代码,可让您更好地理解
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char const *argv[])
{
string fileName = "text.txt";
fstream file;
file.open(fileName, ios::in | ios::out | ios::trunc);
if (file.is_open())
{
file << "I am a Programmer" << endl;
file << "I love to play" << endl;
file << "I love to work game and software development" …Run Code Online (Sandbox Code Playgroud) 我想使用 C++ 标准库工具 ( ) 读取文件std::ifstream- 当然,如果遇到错误,也可以可靠地报告错误。
显然,这并不是一件容易的事!
std::basic_fstream的(其模板std::ifstream是实例化)默认情况下不会抛出异常。basic_ios::exceptions()(这是 的超超类std::ifstream)。14年前,有人问过这个问题:
答案告诉我们:
errno/GetLastError()会为我们提供非零/非成功值。那真是太糟糕了。另一方面,14年过去了。有什么改变吗?也就是说,对于抛出的异常或errno/GetLastError()被设置是否有更好的保证?如果不是,那么在构建中报告错误的“尽力而为”方法是什么std::fstream?
(我很想问“为什么构造函数在失败时不会抛出异常?”,但我们不讨论这个。)