這裏是我的代碼:爲什麼char指針會自動清除?
tmpip = get_public_ip();
pubip = strtok(tmpip, "\n");
sprintf(buffer, "220 FTPUtils Server [%s]", pubip);
len_string = strlen(buffer)+1;
if(send(newsockd, &len_string, sizeof(len_string), 0) < 0){
perror("Errore invio len buffer");
onexit(newsockd, sockd, 0, 2);
}
if(send(newsockd, buffer, len_string, 0) < 0){
perror("Errore durante l'invio");
onexit(newsockd, sockd, 0, 2);
}
pubip = NULL;
free(tmpip);
memset(buffer, 0, sizeof(buffer));
if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
perror("Errore ricezione len user buffer");
onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
perror("Errore ricezione del nome utente");
onexit(newsockd, sockd, 0, 2);
}
user_string = strtok(buffer, " ");
username = strtok(NULL, "\n");
fprintf(stdout, "%s %s\n", user_string, username);
sprintf(saved_user, "%s", username);
memset(buffer, 0, sizeof(buffer));
if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
perror("Errore ricezione len pass buffer");
onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
perror("Errore ricezione password");
onexit(newsockd, sockd, 0, 2);
}
pass_string = strtok(buffer, " ");
password = strtok(NULL, "\n");
fprintf(stdout, "%s %s\n", pass_string, password);
memset(buffer, 0, sizeof(buffer));
printf("%s %s\n", username, password);
我有最後的調試printf
一個問題:它打印到stdout 什麼!
爲什麼我的指針(用戶名和密碼)被清除?
這很奇怪,因爲2個fprintf
作品完美,但最後printf
沒有......
因爲'memset'清零了整個'buffer','username'和'password'指向'buffer'內的特定字節。 – DCoder
你說得對XD喚醒polslinux醒來:D – polslinux
錯誤在這? printf(「%s%s \ n」,用戶名,密碼); –