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

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme