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;
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
add a comment |
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
while (in.good())
is slightly better than the dreadedwhile (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
add a comment |
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
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
c++ getline
edited Mar 9 at 9:41
Khabarkhaan
asked Mar 9 at 3:35
KhabarkhaanKhabarkhaan
13
13
while (in.good())
is slightly better than the dreadedwhile (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
add a comment |
while (in.good())
is slightly better than the dreadedwhile (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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
while (in.good())
is slightly better than the dreadedwhile (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