首页 文章

c ifstream无法读取字符串,并在读取char时出错

提问于
浏览
-2

我有这个写入文件的代码:

cout << "titre = ";
cin >> livre.titre;
cout << "isbn = ";
cin >> livre.isbn;
cout << "auteur = ";
cin >> livre.auteur;
cout << "annee = ";
cin >> livre.annee;
cout << "editeur = ";
cin >> livre.editeur;
cout << "prix = ";
cin >> livre.prix;
cout<<"===================="<<endl;

ofstream write("livres",ios::app);
write.write (( char *)&livre, sizeof livre );
write.close ();}

这一个从文件中读取:

void affiche ()
 {
    livres livre;
    ifstream read ("livres");
    read.read (( char *)&livre,sizeof livre);
    while (read)
    {
        cout << "num : " << livre.num
             << "  | isbn : " << livre.isbn
             << "  | titre : " << livre.titre
             << "  | auteur : " << livre.auteur
             << "  | editeur : " << livre.editeur
             << "  | annee : " << livre.annee
             << "  | prix : " << livre.prix
             << endl;
        read.read (( char *)&livre, sizeof livre);
    }
    read.close ();
}

现在,如果我将变量定义为 char ,如下所示:

char titre[3];
char auteur[3];
char editeur[3];

第一个 cout 将输出所有3个字符 . 例如,如果我用 'abc' 填充 titre ,用 'def' 填充 auteur ,用 'ghi' 填充 editeur ;我从cout获得 titre 的输出是 abcdefghi ,cout的输出是 auteurdefghiediteur 的输出是 ghi . 如果我将我的三个变量定义为 int s,则不会出现此问题 . 当我将这些变量更改为字符串时,编译器运行良好,但exe会粘在第一个字符串cout中 .

这是完整的代码:

class livres{
public:
void creer () {
    cout << "titre = ";
    cin >> livre.titre;
    cout << "isbn = ";
    cin >> livre.isbn;
    cout << "auteur = ";
    cin >> livre.auteur;
    cout << "annee = ";
    cin >> livre.annee;
    cout << "editeur = ";
    cin >> livre.editeur;
    cout << "prix = ";
    cin >> livre.prix;
    ofstream write("livres",ios::app);
    write.write (( char *)&livre, sizeof livre );
    write.close ();}
 void affiche ()
 {
    livres livre;
    ifstream read ("livres");
    read.read (( char *)&livre,sizeof livre);
    while (read)
    {
        cout << "num : " << livre.num
             << "  | isbn : " << livre.isbn
             << "  | titre : " << livre.titre
             << "  | auteur : " << livre.auteur
             << "  | editeur : " << livre.editeur
             << "  | annee : " << livre.annee
             << "  | prix : " << livre.prix
             << endl;
        read.read (( char *)&livre, sizeof livre);
    }
    read.close ();
}

private:
    int isbn;
    char titre[3];
    char auteur[3];
    char editeur[3];
    int annee;
    int prix;
    int num;
};

int main()
{
    livres livre;
    livre.creer();
    livre.affiche();
    return 0;
}

1 回答

  • 1

    您的字符数组在其末尾缺少空字符'\0',这会导致程序读取超出预期字符串的结尾 . 空字符用于标记字符串的结尾,因此任何长度为3的字符串都需要一个长度为4的数组,因此在结尾处有空字符串的空间 . 更多信息:http://www.cplusplus.com/doc/tutorial/ntcs/

    你看到“abcdefghi”,因为你的三个字符数组存储在连续的内存中 . 由于没有'\ 0'字符,程序会读取您的第一个数组并找到接下来的两个数组 .

    你是如何为阵列分配字符的?如果你这样初始化它们:

    char titre[3] = "abc";
    

    您应该看到编译错误

    error: initializer-string for array of chars is too long [-fpermissive] 
        char titre[3] = "abc";
    

    编译器知道“abc”太长,因为它需要第四个空格用于空字符 .

相关问题