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 Answers2

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

Eduardo Pascual Aseff
  • 1,149
  • 2
  • 13
  • 26
  • 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. – Miles Banister Feb 08 '20 at 04:38
  • 1
    @MilesBanister I'm not completely rigth, i should start in 0 not in 1! I'll fixit now – Eduardo Pascual Aseff Feb 08 '20 at 04:39
  • 1
    @EduardoPascualAseff your proposed solution suffers from the [while (!eof())](https://stackoverflow.com/questions/5605125/) problem that needs to be avoided. Use `while (i < 49 && inFile.get(tmp))` instead. – Remy Lebeau Feb 08 '20 at 04:56
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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770