C examples for String:String Console Input
Reads a string and an integer from the keyboard and writes them to a disk file. Then reads the file and displays the information on the screen.
#include <stdio.h> #include <io.h> #include <stdlib.h> int main(void) { FILE *fp;/*from www .j av a 2 s .c o m*/ char s[80]; int t; if ((fp = fopen("test", "w")) == NULL) { printf("Cannot open file.\n"); exit(1); } printf("Enter a string and a number: "); fscanf(stdin, "%s%d", s, &t); /* read from keyboard */ fprintf(fp, "%s %d", s, t); /* write to file */ fclose(fp); if ((fp = fopen("test", "r")) == NULL) { printf("Cannot open file.\n"); exit(1); } fscanf(fp, "%s%d", s, &t); /* read from file */ fprintf(stdout, "%s %d", s, t); /* print on screen */ return 0; }