| | |
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 7:05 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
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
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dissertation dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






