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?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Do Phineas and Ferb ever actually get busted in real time?

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

Why is an old chain unsafe?

A function which translates a sentence to title-case

How can I hide my bitcoin transactions to protect anonymity from others?

Pronouncing Dictionary.com's W.O.D "vade mecum" in English

Can a German sentence have two subjects?

Why is the design of haulage companies so “special”?

"which" command doesn't work / path of Safari?

Example of a relative pronoun

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

Possibly bubble sort algorithm

Why is this code 6.5x slower with optimizations enabled?

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

Can I make popcorn with any corn?

Theorems that impeded progress

Why Is Death Allowed In the Matrix?

Download, install and reboot computer at night if needed

How do I create uniquely male characters?

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

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

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

GPS Rollover on Android Smartphones



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

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page