2

Am trying to read data from a file and store it into a char array. Mostly successful but I get a weird output. The code function looks like this:

char* read_answers() { string fileName, data; char* answer = new char [50]; ifstream inFile; while(!inFile.is_open()){ //cout<<endl<<"Please enter the name of the answers file: "; //cin>>fileName; inFile.open("answers.txt"); if (!inFile.is_open()){ cout << "Error opening file" << endl; } } for (int i = 0; i < 50; i++) { if (inFile.eof()) continue; inFile.get(answer[i]); cout << i << answer[i] << endl; } inFile.close(); cout << answer[0]; cout << answer[1]; cout << answer[2]; return answer; } 

The file looks like this:

C A C A B D D 

and the output looks like this:

0C 1 2A 3 4C 5 6A 7 8B 9 10D 11 12D 13 14═ C A 

To me it looks like it is storing empty data into the array. Is there anyway to skip reading the blank data?

2

2 Answers 2

2

The problem here is with END OF LINE characters (\n).

Try changing your for with this:

int i=0; while (i < 49 && inFile.get(tmp)){ char tmp; inFile.get(tmp); if (tmp != '\n'){ answer[i] = tmp; cout << i << answer[i] << endl; i++; } } answer[i] = '\0'; 

The last line guarantees that the string is well represented in memory

Sign up to request clarification or add additional context in comments.

3 Comments

That fixed it thank you so much. I thought it was the eol's but I didn't quite know how to account for those.
@MilesBanister I'm not completely rigth, i should start in 0 not in 1! I'll fixit now
@EduardoPascualAseff your proposed solution suffers from the while (!eof()) problem that needs to be avoided. Use while (i < 49 && inFile.get(tmp)) instead.
2

You are not taking into account that inFile.get() also reads and returns line break characters, which your loop then outputs. That is why there are blank spaces in your output.

There are other mistakes in your code, such as getting stuck in an endless loop if inFile.open() fails, and misusing inFile.eof().

Try this instead:

char* read_answers() { //string fileName; //cout << "Please enter the name of the answers file: "; //cin >> fileName; ifstream inFile("answers.txt"); if (!inFile.is_open()) { cerr << "Error opening file" << endl; return NULL; } char* answer = new char [51]; char ch; int i = 0; while ((i < 50) && inFile.get(ch)) { if ((ch != ' ') && (ch != '\r') && (ch != '\n')) { cout << i << ch << endl; answer[i] = ch; ++i; } } answer[i] = '\0'; inFile.close(); cout << answer[0]; cout << answer[1]; cout << answer[2]; return answer; } 

Alternatively:

char* read_answers() { //string fileName; //cout << "Please enter the name of the answers file: "; //cin >> fileName; ifstream inFile("answers.txt"); if (!inFile.is_open()) { cerr << "Error opening file" << endl; return NULL; } char* answer = new char [51]; int i = 0; while ((i < 50) && (inFile >> answer[i])) { cout << i << answer[i] << endl; ++i; inFile.ignore(numeric_limits<streamsize>::max(), '\n'); } answer[i] = '\0'; inFile.close(); cout << answer[0]; cout << answer[1]; cout << answer[2]; return answer; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.