有时写入文本文件时会出现错误
Some times getting error while writing in to a text file
我正在为文本文件写一个字符串值
std::string line =('8101001033900039','00','22','','','','','3','','0'),
我们正在写这字符串,如下图
outfile.open((char*)fileName.c_str(), ios::app);
if(outfile.is_open())
{
// write inputted data into the file.
DBGF_TRACE("3.write line : %s", line);
outfile<< line << std::endl;
DBGF_TRACE("write line : %s", line);
}
现在,我遇到的错误是,有时它不会写入。有人可以帮助
一开始是什么
std::string line =('8101001033900039','00','22','','','','','3','','0');
编译器应该给您类似的东西:
prog.cc:8:24: warning: character constant too long for its type
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~~~~~~~~~~~~~~~~
prog.cc:8:43: warning: multi-character character constant [-Wmultichar]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~~
prog.cc:8:48: warning: multi-character character constant [-Wmultichar]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~~
prog.cc:8:53: error: empty character constant
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:56: error: empty character constant
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:59: error: empty character constant
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:62: error: empty character constant
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:69: error: empty character constant
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc: In function 'int main()':
prog.cc:8:43: warning: left operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~~
prog.cc:8:48: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~~
prog.cc:8:53: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:56: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:59: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:62: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:65: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~
prog.cc:8:69: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~
prog.cc:8:72: warning: right operand of comma operator has no effect [-Wunused-value]
std::string line =('8101001033900039','00','22','','','','','3','','0');
^~~
prog.cc:8:71: error: conversion from 'char' to non-scalar type 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} requested
std::string line =('8101001033900039','00','22','','','','','3','','0');
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
所以数据本身不正确。
现在,让我们用双引号替换单引号,因为您可能想要字符串,例如:
std::string line =("8101001033900039","00","22","","","","","3","","0");
然后,只有大多数操作数将被分配给line
,这意味着line
现在等于"0"
。在i =(i, i,1) 1中阅读更多有关它的信息。做?
编辑:
您需要此:
#include <fstream>
#include <string>
#include <iostream>
int main(void) {
std::string fileName = "output.txt";
std::string line ="('8101001033900039','00','22','','','','','3','','0'),";
std::ofstream out(fileName.c_str());
out << line << std::endl;
out.close();
return 0;
}
相关文章:
- 尝试使用 WinBGI 显示文本时出现编译器错误
- ISO 中禁止可变长度数组 C++崇高文本中的错误 3.
- SFML 2.4.2 - 绘制文本时出现分段错误
- C++-从文本文件读取矢量错误
- 在图像上覆盖文本的程序会产生无休止的字符串错误
- c++ 添加具有写入文件错误的文本变量
- 写入文本文件错误,QT
- 收集Visual Studio IntelliSense错误,然后通过脚本将其转储到文本文件中
- 将文本字符串作为常量字符 * 参数传递会导致代码分析器错误
- 用于引发字符串文本C++的编译器错误的宏
- C++文本动画错误 C2397
- 如何将C 编译器错误发送到CMD中的文本文件
- 有时写入文本文件时会出现错误
- QFont 错误地将文本适合特定高度
- 尝试将文本文件加载到动态分配的 2D 数组中时出现'Segmentation fault'错误
- 获取 Poco 返回的错误代码的文本
- 分段错误将文本呈现到开罗上下文
- 从文本文件读取 2D 数组错误
- CMD窗口停止工作,文本文件使用fstream读取错误
- CMake 解析错误函数缺少结尾")"。而是找到带有文本的未终止字符串")