Read int and char from the same file in C
c, file, input
Solution
fscanf(fptr,"%c %d", command, &data);
should be:
fscanf(fptr,"%c %d", &command, &data);
getchar(); // consume the newline character that fscanf left.
Assuming you defined:
char command;
int data;
Problem
I have a text file that looks like this: ``` i 3755 i 3633 i 4435 i 1434 ``` how would I go about reading this as an input, I've tried using fscanf, but it keeps on giving me a random character after the 'i' for example output would look like i% 3755 i5 3633 etc. Here is what I've been trying: ``` int data = 0; char command; if(fptr==NULL) printf("File Cannot Be Read"); fscanf(fptr,"%c %d\n", &command, &data); printf("%c " , command); printf("%d\n" , data); fscanf(fptr,"%s %d\n", &command, &data); printf("%c " , command); printf("%d\n" , data); fscanf(fptr,"%s %d\n", &command, &data); printf("%s " , command); printf("%d\n" , data); ``` none of them seem to work. Thanks for your help in advance! edit: Heres the working code for anybody that was having the same problem: ``` int data = 0; char command; fptr = fopen(argv[1], "r"); if(fptr==NULL) printf("File Cannot Be Read"); while(fscanf(fptr,"%c %d \n", &command, &data) == 2) { if(command == 'i') { printf("insert found\n"); } if(command == 'd') { printf("delete found\n"); } } ``` }