Reading whitespace when asking user input C -
i'm having issues trying read white space character in c. want read user input when asking first , lastly name.
so example, prompt user come in name, type in "peter smith". info, want write file.
when writes file, file reads lastly name "smith". how can read whole string?
here's how asked:
printf("\nplease come in first , lastly name: \n"); scanf("%[^\n]", name); fgets(name, sizeof(name), stdin);
i don't think problem lies in snippet posted. here's illustration programme wrote on linux scheme seek , pinpoint issue:
#include <stdio.h> int main() { char name[128]; int num_scans = 0; file *out = fopen("name.txt", "w"); if(out == null) { printf("failed open file write.\n"); homecoming 1; } printf("\nplease come in first , lastly name: \n"); num_scans = scanf("%127[^\n]", name); if(num_scans < 1) { printf("error reading name.\n"); homecoming 2; } fprintf(out, "%s\n", name); fclose(out); homecoming 0; }
this appeared work me:
$cat name.txt peter smith
if post code used write name file, might reveal source of error.
c user-input
No comments:
Post a Comment