有时写入文本文件时会出现错误

Some times getting error while writing in to a text file

本文关键字:错误 文本 文件      更新时间:2023-10-16

我正在为文本文件写一个字符串值

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;
}