i have a string which is giving me problems. when i put it into a textfile, it puts the cursor onto the nextline. i assume because there is a \n at the end of the string, but when i cout the string, the newline character isnt there (well, the console doesnt put the next peice of text on the newline). is there someway i can stop it adding this newline? because its causing my program to crash later on.
lexusdominus
1
Junior Poster in Training
Recommended Answers
Jump to PostShow the line(s) that you are using to put it into the text file. You may be adding endl to the end of the line when you don't want to. If you're then, reading it back into a string, something like getline reads to the end of the line and …
Jump to PostWhy don't you just get rid of the "\n" in data before writing it into target?
data=data.substr(0,data.length()-1); //if data is of std::string type
Jump to PostNot from what i know.Maybe you could post some more code.
Jump to Post…#include <fstream> #include <string> using namespace std; int main() { string word1 = "dog\n\n"; string word2 = "dog\n"; string word3 = "dog"; ofstream out("dog.txt", ios::app|ios::ate); out << word1 << word2 << word3; out.close(); ofstream out2("dog.txt", ios::app|ios::ate); out2 << word1 << word2 << word3; out2.close(); return 0; }
All 15 Replies
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
lexusdominus
1
Junior Poster in Training
lexusdominus
1
Junior Poster in Training
caut_baia
9
Posting Whiz
lexusdominus
1
Junior Poster in Training
caut_baia
9
Posting Whiz
VernonDozier
2,218
Posting Expert
Featured Poster
lexusdominus
1
Junior Poster in Training
lexusdominus
1
Junior Poster in Training
VernonDozier
2,218
Posting Expert
Featured Poster
lexusdominus
1
Junior Poster in Training
VernonDozier
2,218
Posting Expert
Featured Poster
lexusdominus
1
Junior Poster in Training
VernonDozier
2,218
Posting Expert
Featured Poster
lexusdominus
1
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.