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;
#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.