【问题标题】:Some times getting error while writing in to a text file [closed]有时在写入文本文件时出错[关闭]
【发布时间】:2017-12-05 06:20:34
【问题描述】:

我正在将字符串值连续写入文本文件

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

现在我得到的错误是,有时它无法写入。有人可以帮忙吗

【问题讨论】:

  • 是的..它的 c++ 代码而已
  • 欢迎来到Stack Overflow!请将您的程序缩减为能够说明问题的最小的完整程序。将该程序复制粘贴到您的问题中。请包括您的实际和预期输出。有关更多信息,请参阅minimal reproducible example。此外,tourHow to Ask 中提供了一般信息。
  • AFAIK,'8101001033900039' 不是有效的字符常量,也不是'00'。您在编译时没有收到至少有关此的警告吗?或者您是否打算连接 string 常量? (请编辑您的问题以澄清这一点。)
  • std::string line ="('8101001033900039','00','22','','','','','3','','0' ),";
  • edit 修复代码示例的问题。不要在 cmets 中这样做。

标签: c++ string


【解决方案1】:

开头是什么:

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"。在What does i = (i, ++i, 1) + 1; do?了解更多信息


编辑:

你需要这个:

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

【讨论】:

  • 这些是我预料到的警告......
  • std::ofstream out("output.txt");出
  • outfile.open((char*)fileName.c_str(), std::ofstream::out | std::ofstream::app);就像我现在正在做的那样,我在 outfile.close(); 中遇到运行时错误;
  • 像我的回答@SreenuPolireddy 所说的那样做。
  • @gsamaras 目前将单个记录改正到文件中,但我希望多个记录一个接一个地追加
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2011-09-10
  • 2018-11-05
  • 1970-01-01
相关资源
最近更新 更多