Make textfile all one line without a return to a new line c++
I have the following textfile (essai.txt):
i am a second year student
my account number is 1039
19 1 2019
My desired output:
i am a second year student my account number is 1039 19 1 2019
I am not figuring out how to read the new line return, i have tried getline,tried to read a blank space but obviously it doesn't work..
My code:
char line[50];
ifstream o("essai.txt");
ofstream s("essai.txt");
while (o >> line) {
//Here i am not figuring it out
}
c++
|
show 2 more comments
I have the following textfile (essai.txt):
i am a second year student
my account number is 1039
19 1 2019
My desired output:
i am a second year student my account number is 1039 19 1 2019
I am not figuring out how to read the new line return, i have tried getline,tried to read a blank space but obviously it doesn't work..
My code:
char line[50];
ifstream o("essai.txt");
ofstream s("essai.txt");
while (o >> line) {
//Here i am not figuring it out
}
c++
1
Simple:while (std::getline(o,line)) { s << line << ' '; }Also better declarelineasstd::string line;rather than a rawchararray. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.
– πάντα ῥεῖ
Jan 19 at 15:38
3
Doesn'tofstream s("essai.txt");overwrite the text file?
– melpomene
Jan 19 at 15:40
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46
|
show 2 more comments
I have the following textfile (essai.txt):
i am a second year student
my account number is 1039
19 1 2019
My desired output:
i am a second year student my account number is 1039 19 1 2019
I am not figuring out how to read the new line return, i have tried getline,tried to read a blank space but obviously it doesn't work..
My code:
char line[50];
ifstream o("essai.txt");
ofstream s("essai.txt");
while (o >> line) {
//Here i am not figuring it out
}
c++
I have the following textfile (essai.txt):
i am a second year student
my account number is 1039
19 1 2019
My desired output:
i am a second year student my account number is 1039 19 1 2019
I am not figuring out how to read the new line return, i have tried getline,tried to read a blank space but obviously it doesn't work..
My code:
char line[50];
ifstream o("essai.txt");
ofstream s("essai.txt");
while (o >> line) {
//Here i am not figuring it out
}
c++
c++
edited Jan 19 at 16:12
πάντα ῥεῖ
72.7k974140
72.7k974140
asked Jan 19 at 15:36
Elio Elio
225
225
1
Simple:while (std::getline(o,line)) { s << line << ' '; }Also better declarelineasstd::string line;rather than a rawchararray. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.
– πάντα ῥεῖ
Jan 19 at 15:38
3
Doesn'tofstream s("essai.txt");overwrite the text file?
– melpomene
Jan 19 at 15:40
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46
|
show 2 more comments
1
Simple:while (std::getline(o,line)) { s << line << ' '; }Also better declarelineasstd::string line;rather than a rawchararray. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.
– πάντα ῥεῖ
Jan 19 at 15:38
3
Doesn'tofstream s("essai.txt");overwrite the text file?
– melpomene
Jan 19 at 15:40
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46
1
1
Simple:
while (std::getline(o,line)) { s << line << ' '; } Also better declare line as std::string line; rather than a raw char array. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.– πάντα ῥεῖ
Jan 19 at 15:38
Simple:
while (std::getline(o,line)) { s << line << ' '; } Also better declare line as std::string line; rather than a raw char array. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.– πάντα ῥεῖ
Jan 19 at 15:38
3
3
Doesn't
ofstream s("essai.txt"); overwrite the text file?– melpomene
Jan 19 at 15:40
Doesn't
ofstream s("essai.txt"); overwrite the text file?– melpomene
Jan 19 at 15:40
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46
|
show 2 more comments
1 Answer
1
active
oldest
votes
Here's what you possibly could do successfully:
std::string line;
std::string single_line;
fstream x("essai.txt");
first_iteration = true;
// Read all the data from the file and accumulate the results into a single line
while (std::getline(x,line)) {
if(first_iteration) {
first_iteration = false;
}
else {
single_line += ' ';
}
single_line += line;
}
// Position the file write pointer back to the beginning ...
// (that's most probably not needed at all, since the writing position was
// never changed in the previous code)
x.seekp(std::ios_base:beg);
// ... and write out the formerly accumulated input.
x << single_line;
this is it, thank you!
– Elio
Jan 19 at 15:55
Thefirst_iterationvariable could be removed and the loop be simplified like this:if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}
– zett42
Jan 20 at 18:53
add a comment |
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%2f54268678%2fmake-textfile-all-one-line-without-a-return-to-a-new-line-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's what you possibly could do successfully:
std::string line;
std::string single_line;
fstream x("essai.txt");
first_iteration = true;
// Read all the data from the file and accumulate the results into a single line
while (std::getline(x,line)) {
if(first_iteration) {
first_iteration = false;
}
else {
single_line += ' ';
}
single_line += line;
}
// Position the file write pointer back to the beginning ...
// (that's most probably not needed at all, since the writing position was
// never changed in the previous code)
x.seekp(std::ios_base:beg);
// ... and write out the formerly accumulated input.
x << single_line;
this is it, thank you!
– Elio
Jan 19 at 15:55
Thefirst_iterationvariable could be removed and the loop be simplified like this:if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}
– zett42
Jan 20 at 18:53
add a comment |
Here's what you possibly could do successfully:
std::string line;
std::string single_line;
fstream x("essai.txt");
first_iteration = true;
// Read all the data from the file and accumulate the results into a single line
while (std::getline(x,line)) {
if(first_iteration) {
first_iteration = false;
}
else {
single_line += ' ';
}
single_line += line;
}
// Position the file write pointer back to the beginning ...
// (that's most probably not needed at all, since the writing position was
// never changed in the previous code)
x.seekp(std::ios_base:beg);
// ... and write out the formerly accumulated input.
x << single_line;
this is it, thank you!
– Elio
Jan 19 at 15:55
Thefirst_iterationvariable could be removed and the loop be simplified like this:if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}
– zett42
Jan 20 at 18:53
add a comment |
Here's what you possibly could do successfully:
std::string line;
std::string single_line;
fstream x("essai.txt");
first_iteration = true;
// Read all the data from the file and accumulate the results into a single line
while (std::getline(x,line)) {
if(first_iteration) {
first_iteration = false;
}
else {
single_line += ' ';
}
single_line += line;
}
// Position the file write pointer back to the beginning ...
// (that's most probably not needed at all, since the writing position was
// never changed in the previous code)
x.seekp(std::ios_base:beg);
// ... and write out the formerly accumulated input.
x << single_line;
Here's what you possibly could do successfully:
std::string line;
std::string single_line;
fstream x("essai.txt");
first_iteration = true;
// Read all the data from the file and accumulate the results into a single line
while (std::getline(x,line)) {
if(first_iteration) {
first_iteration = false;
}
else {
single_line += ' ';
}
single_line += line;
}
// Position the file write pointer back to the beginning ...
// (that's most probably not needed at all, since the writing position was
// never changed in the previous code)
x.seekp(std::ios_base:beg);
// ... and write out the formerly accumulated input.
x << single_line;
edited Jan 19 at 16:11
answered Jan 19 at 15:49
πάντα ῥεῖπάντα ῥεῖ
72.7k974140
72.7k974140
this is it, thank you!
– Elio
Jan 19 at 15:55
Thefirst_iterationvariable could be removed and the loop be simplified like this:if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}
– zett42
Jan 20 at 18:53
add a comment |
this is it, thank you!
– Elio
Jan 19 at 15:55
Thefirst_iterationvariable could be removed and the loop be simplified like this:if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}
– zett42
Jan 20 at 18:53
this is it, thank you!
– Elio
Jan 19 at 15:55
this is it, thank you!
– Elio
Jan 19 at 15:55
The
first_iteration variable could be removed and the loop be simplified like this: if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}– zett42
Jan 20 at 18:53
The
first_iteration variable could be removed and the loop be simplified like this: if (std::getline( x, single_line )) { while (std::getline( x, line )) { single_line += ' '; single_line += line; }}– zett42
Jan 20 at 18:53
add a comment |
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%2f54268678%2fmake-textfile-all-one-line-without-a-return-to-a-new-line-c%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
1
Simple:
while (std::getline(o,line)) { s << line << ' '; }Also better declarelineasstd::string line;rather than a rawchararray. If you want to avoid the trailing blank, there are techniques you can use, just distinguishing the first iteration from the following ones and put the blank in front of the output.– πάντα ῥεῖ
Jan 19 at 15:38
3
Doesn't
ofstream s("essai.txt");overwrite the text file?– melpomene
Jan 19 at 15:40
@melpomene Good point.
– πάντα ῥεῖ
Jan 19 at 15:42
You can't write to the file at the same time as you're reading from it (at least it's not advisable to do it that way). Read into a string first, then write only once you've finished all the reading. Or just use two different file names.
– john
Jan 19 at 15:43
it worked having two files one to read from and the other to write on but can't i do it on the same one?
– Elio
Jan 19 at 15:46