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.

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.

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.

It isn't the number 26, its the ascii character that has a decimal value 26. This is written to a text file but is read back as the decimal value 10.

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?

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

Ah, thanks, I understand now :)

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.