为什么 istream get 函数的 none 在此 c 字符串中存储任何内容?
Why do none of the istream get functions store anything in this c-string?
我正在尝试完成将文件中的文本存储到 C 字符串中这一非常简单的任务。出于某种原因,它只是 不工作。
#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;
}
这是 Ch8_Ex6Data.txt
的副本
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
answerkey
完全按照预期的方式工作,但 student[0].id
在 get 函数之后仍然为空白。我试过使用提取运算符 >>
、getline()
和 get()
,但其中 none 确实有效。我做错了什么?
为清楚起见进行编辑:我希望 ABC54102
存储在 student[0].id
中。
这与 C 字符串问题无关。
函数定义:
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 ('[=29=]'
) is automatically appended to the written sequence if n
is greater than zero, even if an empty string is extracted.
问题是数组 char answerKey[20]
不够大。
使用
char answerKey[21];
inFile.getline(answerKey, sizeof(answerKey))
和
//...
char id[9];
//...
inFile.getline(students[0].id, sizeof(id));
也就是说,这不是一个很好的方法,如果您使用 std::getline 会更好。
这是一个quick sample你可以使用并适应你的需要。
注意 I'm not using namespace std.
我正在尝试完成将文件中的文本存储到 C 字符串中这一非常简单的任务。出于某种原因,它只是 不工作。
#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;
}
这是 Ch8_Ex6Data.txt
的副本TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
answerkey
完全按照预期的方式工作,但 student[0].id
在 get 函数之后仍然为空白。我试过使用提取运算符 >>
、getline()
和 get()
,但其中 none 确实有效。我做错了什么?
为清楚起见进行编辑:我希望 ABC54102
存储在 student[0].id
中。
这与 C 字符串问题无关。
函数定义:
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, orn
characters have been written tos
(including the terminating null character).The delimiting character is the newline character (
'\n'
) for the first form, anddelim
for the second: when found in the input sequence, it is extracted from the input sequence, but discarded and not written tos
.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 findingdelim
), the function sets theeofbit
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 tos
. 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 thefailbit
flag is not set (the extracted sequence was exactlyn
characters long).A null character (
'[=29=]'
) is automatically appended to the written sequence ifn
is greater than zero, even if an empty string is extracted.
问题是数组 char answerKey[20]
不够大。
使用
char answerKey[21];
inFile.getline(answerKey, sizeof(answerKey))
和
//...
char id[9];
//...
inFile.getline(students[0].id, sizeof(id));
也就是说,这不是一个很好的方法,如果您使用 std::getline 会更好。
这是一个quick sample你可以使用并适应你的需要。
注意 I'm not using namespace std.