回流垃圾。库特作品...为什么不流?
Ofstream returning garbage. Cout works... Why doesn't ofstream?
本文关键字:为什么不 更新时间:2023-10-16
因此,我正在尝试设计一个程序,该程序输入一个文件,然后读取行,获取每一行并将有关它的信息输出到一个新文件。
我把它都记下来了。。。除了我所有的.txt文件都填充了垃圾,而不是应该填充的内容。
我想不通。如果我把我要输入的字符串放到流中,正确的东西就会在屏幕上打印出来。
fstream lineOutFile;
string newFileName;
stringstream linefile;
linefile << lineCt;
linefile >> newFileName;
newFileName += ".txt";
lineOutFile.open(newFileName.c_str());
if(!lineOutFile)
{
cerr << "Can't open output file.n";
}
stringstream iss;
iss << "The corrected 5' x 3' complement of line " << lineCt <<
" is as follows - nn" << finalSeq << "nnnn" <<
"This line of DNA sequence is made up of " << cgContent <<
" C and G neucleotides.nn" << "It contains " << polyTCount <<
" Poly-T strings of more than 4 consecutive neucleotides. They are as follows. - n" <<
polyTString << "nn There are " << cpgCount <<
" CpG sites. The locations are as follows - n" << cpgString;
string storage;
storage = iss.str();
cout << storage;
lineOutFile << storage;
lineOutFile.close();
lineCt++;
}
我得到了"潣牲捥整✵砠㌠‧"我的.txt文件中有这种疯狂。
当我刚刚吐出同样的刺痛时,我做对了!
为什么我的.txt文件是垃圾?
为什么使用fstream
而不是ofstream
?
fstream
的默认设置是打开现有文件。ofstream
的默认设置是从一个空文件开始。文件中已经存在的内容可能会导致编辑器使用错误的编码来解释数据。
尝试运行
LANG=C your_program
并在编辑器中关闭编码识别。更好的是,使用cat
程序查看您的txt文件。
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 为什么不递增?(构造函数)
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 为什么不允许使用可变长度数组作为向量元素?
- C++:为什么不调用移动构造函数?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 为什么不能用常量表达式声明数组?
- 为什么不能直接引用作用域枚举类成员,而不能为无作用域枚举生成类成员?
- C++ queue.front();为什么不从第一个元素开始呢?
- 为什么不允许这种交叉广播?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 为什么不支持 Xcode 1.5?
- 为什么不能使用带有模板的 lambda
- 为什么不需要在 C++20 中的依赖类型之前指定"typename"?
- C++:如果括号为空,为什么不抛出错误对象?
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?