我需要用c ++编写一个循环文件.程序必须在文件中写入行,当代码达到最大行数时,它必须覆盖文件开头的行.
任何人都有任何想法?
在C++中有没有办法检查ostream对象是对象cout还是ofstream对象?
就像是:
ostream& output(ostream& out)
{
if (out == cout)
return out;
else
{
out << "something different because its not going to the console" << endl;
return out;
}
}
我想这样做的原因是,我想重载<<运算符以执行两个不同的操作,具体取决于它使用的流类型.
是否有可能<<每次使用不同类型的流重载操作员两次?
更新以更好地反映意图.
我正在使用C++输出一个无符号字符数组,ofstream fout("filename");
但它会产生一个虚假字符.这是导致问题的代码的一部分:
for(int i = 0; i < 12; i++)
fout << DChuffTable[i];
Run Code Online (Sandbox Code Playgroud)
这是数组的定义:
unsigned char DChuffTable[12] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B};
Run Code Online (Sandbox Code Playgroud)
在输出文件,我收到了虚假0x0D之间0x09和0x0A.我在调试模式中检查了数组,然后才开始打印并且没有更改.请告诉我你对这个问题的看法.
我正在运行一些基准测试,以找到最有效的方法将大型数组写入C++文件(ASCII中超过1Go).
所以我将std :: ofstream与fprintf进行了比较(参见下面我使用的开关)
case 0: {
std::ofstream out(title, std::ios::out | std::ios::trunc);
if (out) {
ok = true;
for (i=0; i<M; i++) {
for (j=0; j<N; j++) {
out<<A[i][j]<<" ";
}
out<<"\n";
}
out.close();
} else {
std::cout<<"Error with file : "<<title<<"\n";
}
break;
}
case 1: {
FILE *out = fopen(title.c_str(), "w");
if (out!=NULL) {
ok = true;
for (i=0; i<M; i++) {
for (j=0; j<N; j++) {
fprintf(out, "%d ", A[i][j]);
}
fprintf(out, "\n");
}
fclose(out);
} else …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile ("input.dat", ios::in);
ofstream ofile ("output.dat",ios::out);
int num;
ifile >> num;
ofile << num;
ofile << endl;
ofile << "Did we go to new line?";
ofile << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,output.dat中的所有内容都在同一行.我该如何解决这个问题?
谢谢!
编辑:我使用Windows来查看文件和Linux进行编译.这就是我遇到这个问题的原因.使用cat output.dat在Linux中看到文件的内容会发现,Windows与Linux的换行符是在不同的时间.
故意我有这个写入文件的方法,所以我试图处理我写入封闭文件的可能性的异常:
void printMe(ofstream& file)
{
try
{
file << "\t"+m_Type+"\t"+m_Id";"+"\n";
}
catch (std::exception &e)
{
cout << "exception !! " << endl ;
}
};
Run Code Online (Sandbox Code Playgroud)
但显然std :: exception不是关闭文件错误的合适例外,因为我故意尝试在已经关闭的文件上使用此方法,但我的"异常!!"注释未生成.
那么我应该写什么例外?
我正在尝试使用std::ofstream::write方法编写二进制文件.我发现,有些字符不是按原样写的,例如:
std::ofstream in("testout");
int i =10;
in.write((const char *)(&i), sizeof(i));
in.close();
return 0;
Run Code Online (Sandbox Code Playgroud)
将以下内容写入二进制文件:0d 0a 00 00 00
为什么会出现其他0d字节?
我认为这应该很简单,但到目前为止我的谷歌搜索没有帮助...我需要用C++写入现有文件,但不一定在文件的末尾.
我知道当我只想将文本附加到我的文件时,我可以ios:app在调用open我的流对象时传递该标志.但是,这只是让我写到文件的最后,而不是写到它的中间.
我做了一个简短的程序来说明这个问题:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string path = "../test.csv";
fstream file;
file.open(path); // ios::in and ios::out by default
const int rows = 100;
for (int i = 0; i < rows; i++) {
file << i << "\n";
}
string line;
while (getline(file, line)) {
cout << "line: " << line << endl; // here I would like to append more text to certain rows
}
file.close(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用std :: ofstream()编写UTF-16编码文件.即使在二进制模式下写入"\n\0"也是如此"\r\n\0".示例代码:
std::string filename = ...
std::ofstream fout(filename, std::ios_base::binary);
fout.write("\xff\xfe", 2);
fout.write("\n\0", 2);
fout.close();
Run Code Online (Sandbox Code Playgroud)
生成的文件的十六进制数据是:
ff fe 0d 0a 00
Run Code Online (Sandbox Code Playgroud)
我一定做错了什么.有什么想法阻止写入0x0d?
我正在使用MS VisualStudio 2013.
更新:莫名其妙地开始按预期工作.把它归结为机器中的幽灵.
根据此C ++参考:http : //www.cplusplus.com/reference/fstream/ofstream/ofstream/ ,的默认打开模式std::ofstream是ios_base::out,并且没有提及其他隐式模式。因此,我希望如果用小文件覆盖大文件,则大文件的“超出”部分应保持不变,并且仅文件的第一部分应由较短的新数据代替。
另一方面,《 Apache C ++标准库用户指南》(http://stdcxx.apache.org/doc/stdlibug/30-3.html)在第30.3.1.2段的注释中指出:“对于输出文件流,打开模式输出等效于out | trunc,也就是说,您可以省略trunc标志。但是,对于双向文件流,必须始终明确指定trunc。
我已经试过这段代码:
#include <fstream>
int main()
{
std::ofstream aFileStream("a.out", std::ios_base::out);
aFileStream << "Hello world!";
aFileStream.close();
std::ofstream aFileStream2("a.out", std::ios::out);
aFileStream2 << "Bye!";
aFileStream2.close();
}
Run Code Online (Sandbox Code Playgroud)
无论是Windows上的g ++ 8.1和Linux上的g ++ 6.3,Apache文档似乎都是正确的。大文件被截断,在第二个文件流中写入较短的字符串后,没有任何内容。
为什么会这样呢?cplusplus.com错误吗?抑或行为令人沮丧?