Unwanted nul character in getline() resultWhy is iostream::eof inside a loop condition considered wrong?std::getline() returnsHow do I alter this tokenization process to work on a text file with multiple lines?Getline and 16h (26d) characterGetline in c++ ifstream causing unwanted behavoir?getline C++ not getting result that is required character in istream::getline()Getline is reading strings as charactersShift-JIS decoding fails using wifstrem in Visual C++ 2013Issues with getline c++How to write from text file to array and from array to text file?

What typically incentivizes a professor to change jobs to a lower ranking university?

How can bays and straits be determined in a procedurally generated map?

Email Account under attack (really) - anything I can do?

How to add power-LED to my small amplifier?

Is there really no realistic way for a skeleton monster to move around without magic?

How is it possible to have an ability score that is less than 3?

Can I use wish to become the ruler of all dragons?

The use of multiple foreign keys on same column in SQL Server

Basic combinations logic doubt in probability

Explain the parameters before and after @ in the terminal prompt

Draw simple lines in Inkscape

Why are weather verbs 曇る and 晴れる treated differently in this sentence?

What do you call a Matrix-like slowdown and camera movement effect?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Why was the small council so happy for Tyrion to become the Master of Coin?

Is the month field really deprecated?

How to get the available space of $HOME as a variable in shell scripting?

Why has Russell's definition of numbers using equivalence classes been finally abandonned? ( If it has actually been abandonned).

How is it possible for user to changed after storage was encrypted? (on OS X, Android)

Dragon forelimb placement

How to re-create Edward Weson's Pepper No. 30?

Underlining section titles

Can I make popcorn with any corn?

A Journey Through Space and Time



Unwanted nul character in getline() result


Why is iostream::eof inside a loop condition considered wrong?std::getline() returnsHow do I alter this tokenization process to work on a text file with multiple lines?Getline and 16h (26d) characterGetline in c++ ifstream causing unwanted behavoir?getline C++ not getting result that is required character in istream::getline()Getline is reading strings as charactersShift-JIS decoding fails using wifstrem in Visual C++ 2013Issues with getline c++How to write from text file to array and from array to text file?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-2















I have a simple code as bellow for reading and writing contents of a unicode text file:



#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()

std::vector<std::wstring> words;
std::wifstream in;
in.open("test.txt", ios_base::in);
if (in.is_open())

std::wstring word;
while (in.good())

getline(in, word);
words.push_back(word);


else
cout << "Cannot open file" << endl;

std::wofstream out;
out.open("result.txt", ios_base::out);

for (size_t i = 0; i < words.size(); ++i)

out << words[i] << endl;

out.close();
return 0;



In the result file, at the binning of each line (except for first line), a nul character will be inserted!
Please help me what is wrong in the code.
Thanks in advance!



Update



For better expression, when the test.txt file contains as bellow:



Hi
This is a test!


the result.txt will be as this picture:
enter image description here



As can be shown from image, a nul character will appear in the text!



if using this line



out << words[i] << endl;


instead of



out.write(words[i].c_str(), words[i].size() + 1);


the problem will grow! The Chines letters will appear in the result.txt
The test text:



Hi
This is a test code!
What is wrong?


The result:
enter image description here



On Windows OS, the code compiles with Microsoft Visual Studio and Intel Compiler. Both of them have the same behavior.
But on Android, Mobile C App run the program with no problem!



Can anybody help me?










share|improve this question
























  • while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

    – user4581301
    Mar 9 at 4:26











  • Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

    – Khabarkhaan
    Mar 9 at 9:05

















-2















I have a simple code as bellow for reading and writing contents of a unicode text file:



#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()

std::vector<std::wstring> words;
std::wifstream in;
in.open("test.txt", ios_base::in);
if (in.is_open())

std::wstring word;
while (in.good())

getline(in, word);
words.push_back(word);


else
cout << "Cannot open file" << endl;

std::wofstream out;
out.open("result.txt", ios_base::out);

for (size_t i = 0; i < words.size(); ++i)

out << words[i] << endl;

out.close();
return 0;



In the result file, at the binning of each line (except for first line), a nul character will be inserted!
Please help me what is wrong in the code.
Thanks in advance!



Update



For better expression, when the test.txt file contains as bellow:



Hi
This is a test!


the result.txt will be as this picture:
enter image description here



As can be shown from image, a nul character will appear in the text!



if using this line



out << words[i] << endl;


instead of



out.write(words[i].c_str(), words[i].size() + 1);


the problem will grow! The Chines letters will appear in the result.txt
The test text:



Hi
This is a test code!
What is wrong?


The result:
enter image description here



On Windows OS, the code compiles with Microsoft Visual Studio and Intel Compiler. Both of them have the same behavior.
But on Android, Mobile C App run the program with no problem!



Can anybody help me?










share|improve this question
























  • while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

    – user4581301
    Mar 9 at 4:26











  • Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

    – Khabarkhaan
    Mar 9 at 9:05













-2












-2








-2


0






I have a simple code as bellow for reading and writing contents of a unicode text file:



#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()

std::vector<std::wstring> words;
std::wifstream in;
in.open("test.txt", ios_base::in);
if (in.is_open())

std::wstring word;
while (in.good())

getline(in, word);
words.push_back(word);


else
cout << "Cannot open file" << endl;

std::wofstream out;
out.open("result.txt", ios_base::out);

for (size_t i = 0; i < words.size(); ++i)

out << words[i] << endl;

out.close();
return 0;



In the result file, at the binning of each line (except for first line), a nul character will be inserted!
Please help me what is wrong in the code.
Thanks in advance!



Update



For better expression, when the test.txt file contains as bellow:



Hi
This is a test!


the result.txt will be as this picture:
enter image description here



As can be shown from image, a nul character will appear in the text!



if using this line



out << words[i] << endl;


instead of



out.write(words[i].c_str(), words[i].size() + 1);


the problem will grow! The Chines letters will appear in the result.txt
The test text:



Hi
This is a test code!
What is wrong?


The result:
enter image description here



On Windows OS, the code compiles with Microsoft Visual Studio and Intel Compiler. Both of them have the same behavior.
But on Android, Mobile C App run the program with no problem!



Can anybody help me?










share|improve this question
















I have a simple code as bellow for reading and writing contents of a unicode text file:



#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()

std::vector<std::wstring> words;
std::wifstream in;
in.open("test.txt", ios_base::in);
if (in.is_open())

std::wstring word;
while (in.good())

getline(in, word);
words.push_back(word);


else
cout << "Cannot open file" << endl;

std::wofstream out;
out.open("result.txt", ios_base::out);

for (size_t i = 0; i < words.size(); ++i)

out << words[i] << endl;

out.close();
return 0;



In the result file, at the binning of each line (except for first line), a nul character will be inserted!
Please help me what is wrong in the code.
Thanks in advance!



Update



For better expression, when the test.txt file contains as bellow:



Hi
This is a test!


the result.txt will be as this picture:
enter image description here



As can be shown from image, a nul character will appear in the text!



if using this line



out << words[i] << endl;


instead of



out.write(words[i].c_str(), words[i].size() + 1);


the problem will grow! The Chines letters will appear in the result.txt
The test text:



Hi
This is a test code!
What is wrong?


The result:
enter image description here



On Windows OS, the code compiles with Microsoft Visual Studio and Intel Compiler. Both of them have the same behavior.
But on Android, Mobile C App run the program with no problem!



Can anybody help me?







c++ getline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 9:41







Khabarkhaan

















asked Mar 9 at 3:35









KhabarkhaanKhabarkhaan

13




13












  • while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

    – user4581301
    Mar 9 at 4:26











  • Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

    – Khabarkhaan
    Mar 9 at 9:05

















  • while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

    – user4581301
    Mar 9 at 4:26











  • Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

    – Khabarkhaan
    Mar 9 at 9:05
















while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

– user4581301
Mar 9 at 4:26





while (in.good()) is slightly better than the dreaded while (in.eof()), but it still tests for whether the data is good or not BEFORE reading and using the data. You need to read, test, and then use (or reject) the data. Any other order leaves the program open to one bug or another.

– user4581301
Mar 9 at 4:26













Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

– Khabarkhaan
Mar 9 at 9:05





Thanks for your response. The problem is on Windows, I tested the code on Visual Studio compiler, Intel compiler and Mobile C for Android. on Android the code has no problem but on Windows it encounters to mentioned issue.

– Khabarkhaan
Mar 9 at 9:05












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073740%2funwanted-nul-character-in-getline-result%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073740%2funwanted-nul-character-in-getline-result%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Zrinski family tree See also Navigation menu

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?