프로그램이 사용자에게 사용자 이름을 묻는 메시지가 표시됩니다. 사용자 이름을 받으면이를 '.history'와 연결하여 username.history를 만듭니다. 그런 다음 해당 파일 (username.history)을 열고 그 파일에서 입력을 읽습니다. 나는 여기에 segfault를 달리고있다. 파일이 존재하지 않아 비어있는 파일을 열 때마다 여러 줄을 읽은 다음 segfault를 던집니다. 문제는 파일을 열려고하는 방법에서 비롯된 것 같지만 확실하지 않습니다. 여기서 문제가 발생되는 부분이다 :파일 읽기 중 Segfault
// 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를 어떤 도움이 크게 (숯 cmd를 [200] [50])
appreaciated한다 글로벌 변수로 선언되어있다.
"username.history"의 각 줄은 50 자 미만입니까? – Falmarri
파일은 처음 실행시 존재하지 않으므로 처음에는 문제가되지 않습니다. – Bigby
왜 C++ 프로그램에서'strcat' (C 프로그래머가 안전하지 않다고 생각합니까?)를 사용하고 있습니까? 'std :: string'을 사용하면 인생이 갑자기 훨씬 쉬워 질 것입니다. –