| | |
Reading in new line character from a file.
![]() |
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
Hi, I'm trying to read a series of ascii characters from a text file as their corresponding decimal integers. However when I try to read in the # 26 it is read in as a # 10 and no further characters after this are read. I've found out that # 26 represents a substitution character and # 10 is a new line (http://www.asciitable.com/)
This is the code I'm using to illustrate my problem;
Any ideas what I can do?
Thankyou for any reply.
This is the code I'm using to illustrate my problem;
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { // fill up the vector with 4 integers vector <int> vec; vec.push_back(21); vec.push_back(23); vec.push_back(26); vec.push_back(29); cout << "Numbers in vector are:" << endl; for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } //write out the vector contents as chars to a text file ofstream outfile("test.txt"); if (outfile.fail()) { cout << "test.txt" << endl; return 1; } for (int g = 0; g < vec.size(); g++) { outfile << char(vec[g]); } outfile.close(); //now read the chars back in as the ints [21, 23, 26, 29] string str, tmp; ifstream infile("test.txt"); if (infile.fail()) { cout << "Error opening test.txt" << endl; return 1; } while (!infile.eof()) { getline(infile, tmp); str += tmp; str += "\n"; } infile.close(); cout << endl << "String read in is:" <<endl; for (int g=0; g<str.size(); g++) { cout << int(str[g]) << " "; } cout << endl; }
Any ideas what I can do?
Thankyou for any reply.
The input file is opened in text mode, which means your program will never see the '\n' character(s) because getline() will filter it out, leaving only the characters up to but not including '\n'.
That read loop is wrong anyway because eof() doesn't work that way.
I don't know what you mean by reading # 26. Does that file contain binary information? If yes, then it isn't a text file.
That read loop is wrong anyway because eof() doesn't work that way.
C++ Syntax (Toggle Plain Text)
while (getline(infile, tmp)) { str += tmp; str += "\n"; }
I don't know what you mean by reading # 26. Does that file contain binary information? If yes, then it isn't a text file.
Last edited by Ancient Dragon; Oct 22nd, 2008 at 6:05 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
test.txt is a binary file, not a text file so getline() doesn't know how to read it. Then you are putting all that binary stuff into a std::string and ucing cout to display it -- cout can't because it doesn't know how to display binary data when converted to char.
Use a hex editor on that test.txt file and it contains the 4 values that were written.
Maybe this is what you want?
Use a hex editor on that test.txt file and it contains the 4 values that were written.
Maybe this is what you want?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; int main() { // fill up the vector with 4 integers vector <int> vec; vec.push_back(21); vec.push_back(23); vec.push_back(26); vec.push_back(29); cout << "Numbers in vector are:" << endl; for (size_t i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } //write out the vector contents as chars to a text file ofstream outfile("test.txt"); if (outfile.fail()) { cout << "test.txt" << endl; return 1; } for (size_t g = 0; g < vec.size(); g++) { outfile << char(vec[g]); } outfile.close(); vec.erase(vec.begin(),vec.end()); //now read the chars back in as the ints [21, 23, 26, 29] ifstream infile("test.txt", ios::binary); if (infile.fail()) { cout << "Error opening test.txt" << endl; return 1; } char c; std::string str; while (infile.good()) { c = infile.get(); std::stringstream sstm; sstm << c; str += sstm.str(); } infile.close(); cout << endl << "String read in is:" <<endl; for (size_t g=0; g<str.size(); g++) { cout << int(str[g]) << " "; } cout << endl; }
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Similar Threads
- fstream Tutorial (C++)
- Difficulty with IO (C++)
- reading a line in excel (C)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- Reading in a text file string is not complete (Pascal and Delphi)
- Need help reading in a file (C++)
- reading from a file with python (Python)
- noobie asking for assistance with file parsing... (Perl)
- reading a same input twice from the console (C)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: Write from CPP program to serial port
- Next Thread: factorial
Views: 2686 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes client code compile compiler constructor conversion convert count delete dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework iamthwee input int lazy link linked-list linker list loop looping loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference search sockets sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual win32 window windows winsock wxwidgets






