0

I'm trying to accomplish the very simple task of storing text from a file into a c-string. For some reason, it's just not working.

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct student
{
    char id[8];
    char responses[20];
    int score;
    double grade;
};

int main()
{
    ifstream inFile("Ch8_Ex6Data.txt");

    char answerKey[20];
    inFile.getline(answerKey, 20);

    student students[256];

    inFile.getline(students[0].id, 8);

    cout << answerKey << endl;
    cout << students[0].id;

    return 0;
}

Here's a copy of Ch8_Ex6Data.txt

TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF

answerkey works exactly the way it's supposed to, but student[0].id remains blank after the get function. I've tried using the extraction operator >>, getline(), and get(), but none of them actually work. What am I doing wrong?

Edit for clarity: I want ABC54102 to be stored in student[0].id.

  • 2
    You aren't accounting for the null terminator in `id`. A `char[n]` can only store a string of `n-1` characters if you are using null terminated strings. – François Andrieux Apr 15 '20 at 15:10
  • 1
    Also... why do you wish to store this in a c-string? `std::string` can be converted to a `const char*` with `std::string::c_str()` so why not just use that? – Object object Apr 15 '20 at 15:12

1 Answers1

0

This doesn't relate to a C-string problem.

Function definition:

istream& getline (char* s, streamsize n );

istream& getline (char* s, streamsize n, char delim );

Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

The delimiting character is the newline character ('\n') for the first form, and delim for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written to s.

The function will also stop extracting characters if the end-of-file is reached. If this is reached prematurely (before either writing n characters or finding delim), the function sets the eofbit flag.

The failbit flag is set if the function extracts no characters, or if the delimiting character is not found once (n-1) characters have already been written to s. Note that if the character that follows those (n-1) characters in the input sequence is precisely the delimiting character, it is also extracted and the failbit flag is not set (the extracted sequence was exactly n characters long).

A null character ('\0') is automatically appended to the written sequence if n is greater than zero, even if an empty string is extracted.

The issue is that the array char answerKey[20] is not big enough.

Use

char answerKey[21];
inFile.getline(answerKey, sizeof(answerKey))

and

//...
char id[9];
//...
inFile.getline(students[0].id, sizeof(id));

See this sample

That being said, this is not a very good method, it would be preferable if you used std::getline instead.

Here is a quick sample you can use and adapt to your needs.

Note that I'm not using namespace std.

Community
  • 1
  • 1
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Couldn't I just `ignore` the `'\n'`? – Curtice Gough Apr 15 '20 at 15:26
  • The way you are using getline it will act like the default delimiter, and will be discarded, so you don't need to ignore it, what you need is for the char array to be long enough to store the 20 characters + the null terminator which is needed for it to be treated as a string. – anastaciu Apr 15 '20 at 15:30
  • Also worth noting: If the array isn't big enough, the steam is placed in fail state and needs to be `clear`ed. Always check the stream state after an IO transaction to see if the transaction was successful. In general checking the diagnostic information makes chasing bugs a lot easier. – user4581301 Apr 15 '20 at 15:59
  • @user4581301, yes, it's definitely worth mentioning, it's precisely the source of the problem, I added the rest of the definition of the function to the answer. – anastaciu Apr 15 '20 at 16:13