Working on more example usages of io functions for ch25

This commit is contained in:
Dan Buch 2011-11-10 19:56:32 -05:00
parent 912eb6a143
commit b5b0213c5d

View File

@ -2,16 +2,56 @@
#include "dbg.h" #include "dbg.h"
int main(int argc, char *argv[]) int fscanf_example01()
{ {
printf("Reading from stdin: <a:number> <b:number> <c:letter>\n");
int a, b, rc; int a, b, rc;
char c; char c;
printf("Reading from stdin: <a:number> <b:number> <c:letter>\n> ");
rc = fscanf(stdin, "%d %d %c", &a, &b, &c); rc = fscanf(stdin, "%d %d %c", &a, &b, &c);
check(rc == 3, "Failed to read from stdin."); check(rc == 3, "Failed to read from stdin.");
printf("Read in: a=%d b=%d c=%c\n", a, b, c); printf("Read in: a=%d b=%d c=%c\n", a, b, c);
return 1;
error:
return 0;
}
int fscanf_example02()
{
int a, b, rc;
char c;
char filename[1024];
FILE *fp;
printf("Provide filename from which to inputs\n> ");
rc = fscanf(stdin, "%s", filename);
check(rc == 1, "Failed to read filename");
fp = fopen(filename, "r");
check(fp != NULL, "Failed to open file %s", filename);
printf("Reading from \"%s\": <a:number> <b:number> <c:letter>\n", filename);
rc = fscanf(fp, "%d %d %c", &a, &b, &c);
check(rc == 3, "Failed to read from \"%s\".", filename);
printf("Read in: a=%d b=%d c=%c\n", a, b, c);
rc = fclose(fp);
check(rc != EOF, "Failed to close file \"%s\"", filename);
return 1;
error:
return 0;
}
int main(int argc, char *argv[])
{
check(fscanf_example01() == 1, "Fail!");
check(fscanf_example02() == 1, "Fail!");
return 0; return 0;
error: error: