-5

i am writing a string value to a text file continuously

std::string line =('8101001033900039','00','22','','','','','3','','0'),

this string we are writing to a text file like below

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

Now the error i am getting is , sometimes its getting failed to write . Can someone help

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • yah.. its c++ code only – Sreenu Polireddy Dec 05 '17 at 06:22
  • 1
    Welcome to [SO]! Please reduce your program to the smallest possible **complete** program that demonstrates the problem. copy-paste that program into your question. Please include your actual and expected output. See [mcve] for more information. Also, general information is availble in [tour] and [ask]. – Robᵩ Dec 05 '17 at 06:27
  • 2
    AFAIK, `'8101001033900039'` is not a valid character constant nor `'00'`. Didn't you get at least warnings about this when compiling? Or did you intend to concatenate _string_ constants? (Please, edit your question to clarify this.) – Scheff's Cat Dec 05 '17 at 06:27
  • std::string line ="('8101001033900039','00','22','','','','','3','','0'),"; – Sreenu Polireddy Dec 05 '17 at 06:30
  • its the data getting from server – Sreenu Polireddy Dec 05 '17 at 06:30
  • 1
    Please, [edit] the question to fix the code sample. Don't do this in comments. – Scheff's Cat Dec 05 '17 at 06:32
  • Probably related: [SO: What could cause a stream to enter the “bad” state?](https://stackoverflow.com/q/12161728/7478597) – Scheff's Cat Dec 05 '17 at 06:39
  • Getting error??? Which one? From OS, ios::bad, ios::fail, segfault????? – Klaus Dec 05 '17 at 06:39
  • Yet another related: [SO: Error handling in std::ofstream while writing data](https://stackoverflow.com/q/28342660/7478597) – Scheff's Cat Dec 05 '17 at 06:42
  • Unrelated: `std::endl` inserts an end of line into the stream and flushes the stream to the underlying media. In this case the media is a file, and that means writing to a file. This can be sssssslllllllloooooooowwwwwww, so it's in your best interest to NOT flush until you're forced to. Consider using `outfile<< line << '\n';` – user4581301 Dec 05 '17 at 07:09
  • added outfile<< line << '\n'; but here outfile.close() is giving run time error – Sreenu Polireddy Dec 05 '17 at 09:57

1 Answers1

2

What is that to begin with:

std::string line =('8101001033900039','00','22','','','','','3','','0');

The compiler should give you something like that:

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');
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~

so the data themselves are not OK.

So now, let's replace single quotes with double quotes, since you probably want strings, like this:

std::string line =("8101001033900039","00","22","","","","","3","","0");

then only the right most operand will be assigned to line, meaning that line is now equal to "0". Read more about it in What does i = (i, ++i, 1) + 1; do?


EDIT:

You need this:

#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;
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305