file.txt的内容是:
5 3
6 4
7 1
10 5
11 6
12 3
12 4
Run Code Online (Sandbox Code Playgroud)
5 3坐标对在哪里.如何在C++中逐行处理此数据?
我能够得到第一行,但是如何获得文件的下一行?
ifstream myfile;
myfile.open ("text.txt");
Run Code Online (Sandbox Code Playgroud) 可能重复:
我是否需要手动关闭ifstream?
我是否需要调用fstream.close()或是fstream一个正确的RAII对象,在销毁时关闭流?
我std::ofstream在方法中有一个本地对象.我可以假设退出此方法后文件总是关闭而不调用close吗?我找不到析构函数的文档.
我目前从一个向量写一组双打到一个文本文件,如下所示:
std::ofstream fout;
fout.open("vector.txt");
for (l = 0; l < vector.size(); l++)
fout << std::setprecision(10) << vector.at(l) << std::endl;
fout.close();
Run Code Online (Sandbox Code Playgroud)
但这需要花费大量时间才能完成.有没有更快或更有效的方法来做到这一点?我很乐意看到并学习它.
我目前正在使用std::ofstream如下:
std::ofstream outFile;
outFile.open(output_file);
Run Code Online (Sandbox Code Playgroud)
然后我尝试将std::stringstream对象传递给outFile如下:
GetHolesResults(..., std::ofstream &outFile){
float x = 1234;
std::stringstream ss;
ss << x << std::endl;
outFile << ss;
}
Run Code Online (Sandbox Code Playgroud)
现在我只outFile包含垃圾:"0012E708"重复了一遍.
在GetHolesResults我可以写
outFile << "Foo" << std:endl;
Run Code Online (Sandbox Code Playgroud)
并且它将正确输出outFile.
关于我做错了什么的任何建议?
我正在使用C++ 在Qt应用程序中实现文件保存功能.
我正在寻找一种方法来检查所选文件是否已经存在,然后写入它,以便我可以向用户提示警告.
我正在使用std::ofstream,我不是在寻找Boost解决方案.
有没有办法将输出流作为参数传递
void foo (std::ofstream dumFile) {}
我试过了,但它给了
error : class "std::basic_ofstream<char, std::char_traits<char>>" has no suitable copy constructor
我有一个类似于下面的函数,它是const,需要在继续之前检查文件流是否打开:
bool MyClass::checkSomeStuff() const
{
// Where outputFile_ is a std::ofstream
if ( ! outputFile_.is_open() )
{
throw std::runtime_error( "Output file not open." );
}
// ... do more stuff
Run Code Online (Sandbox Code Playgroud)
但是,似乎我不能这样做,因为is_open()声明为:
bool is_open ( );
Run Code Online (Sandbox Code Playgroud)
(即非常数)
对我来说,像这样的函数 - 显然是一个纯粹的访问器 - 似乎有点奇怪 - 应该是非常量的.背后的逻辑是否合理?
我正试图找到一种方法将fout或cout传递给函数.我意识到有一些逻辑上简单的方法可以解决这个问题,例如将ifs放在任何输出数据的函数中,或者甚至只是双向编写函数.然而,这似乎是原始的和低效的.我不相信这段代码会起作用,我把它放在这里是为了确保我很容易看到我喜欢做什么.请注意,我正在使用c ++进行算法设计课程,我绝不是一个经验丰富的c ++程序员.我的课程仅限于使用您看到的标题.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void helloWorld(char);
ofstream fout;
int main()
{
fout.open("coutfout.dat");
helloWorld(c);
helloWorld(f);
return 0;
}
void helloWorld(char x)
{
xout << "Hello World";
return;
}
Run Code Online (Sandbox Code Playgroud) fwrite()C中的函数const void *restrict buffer用作第一个参数,因此您可以直接将指针struct作为第一个参数传递给您.
http://en.cppreference.com/w/c/io/fwrite
例如fwrite(&someStruct, sizeof(someStruct), 1, file);
但在C++中,ostream::write()需求const char_type*会迫使您使用reinterpret_cast.(在Visual Studio 2013中,它是const char*.)
http://en.cppreference.com/w/cpp/io/basic_ostream/write
egfile.write(reinterpret_cast<char*>(&someStruct), sizeof(someStruct));
几乎在所有情况下,要写入文件的二进制数据都不是char数组,那么为什么标准更喜欢看起来更复杂的样式呢?
PS
1.实际上我write()在ofstreamwith ios::binary模式下使用了这个方法,但根据引用,它继承了ofstream.所以我ostream::write()在上面使用.
2.如果要打印字符流,可以使用operator<<().是不是write()为编写原始数据而设计的方法?
3.如果write()不是编写二进制数据的方法,那么在标准中执行此操作的方法是什么?(尽管由于不同平台上的各种内存对齐策略,这可能会影响代码的可移植性)
我想确保已将一个ofstream写入磁盘设备.这样做的便携方式(可在POSIX系统上移植)是什么?
如果我open将文件分别以只读附加模式获取文件描述符并fsync用它调用,那么这是否解决了问题?像这样:
ofstream out(filename);
/* ...
write content into out
...
*/
out.close();
int fd = open(filename, O_APPEND);
fsync(fd);
close(fd);
Run Code Online (Sandbox Code Playgroud)