0

プログラムは、ユーザーにユーザー名の入力を求めることになっています。ユーザー名を受け取ると、それを「.history」と連結して username.history を作成します。次に、そのファイル (username.history) を開き、そこから入力を読み取ります。ただし、ここでセグメンテーション違反が発生しています。ファイルが存在しないために空であるファイルを開くたびに、複数の行を読み取り、セグメンテーション違反をスローします。問題は、ファイルを開こうとしている方法に起因する可能性があると思いますが、よくわかりません。問題を引き起こしている部分は次のとおりです。

// File input and output
ifstream f_in;
ofstream f_out;

// Prompt user for their username.
char username[80];
cout << "Please input your username: " << endl;
cin >> username;
cout << endl;
cout << "Loading history file if it exists." << endl;

// Create file naem and initialize the file line counter to 0.
strcat(username, ".history");
int fcount = 0;

// Open file and read in lines if there are any.
// Place read lines into the command string for use later.
char tmp[50];

f_in.open(username);
while(!f_in.eof()){
    f_in >> tmp;
    cmd[fcount] = tmp;
    fcount++;
}
f_in.close();

その他の関連情報: cmd はグローバル変数として宣言されています (char cmd[200][50])

どんな助けでも大歓迎です。

4

2 に答える 2

2

それが唯一の問題かどうかはわかりませんが、cmd[fcount] = tmp間違っています。を使用する必要がありますstrcpy()

于 2012-05-31T22:56:20.757 に答える
0
while(f_in.good())
{
    f_in >> tmp;
    cmd[fcount] = tmp;
    fcount++;
}
于 2012-05-31T22:57:46.157 に答える