|
|
1.1 ! root 1: ! 2: ! 3: fscanf() STDIO fscanf() ! 4: ! 5: ! 6: ! 7: ! 8: Format input from a file stream ! 9: ! 10: #include <stdio.h> ! 11: iinntt ffssccaannff(_f_p, _f_o_r_m_a_t, _a_r_g_1, ... _a_r_g_N) ! 12: FFIILLEE *_f_p; cchhaarr *_f_o_r_m_a_t; ! 13: [_d_a_t_a _t_y_p_e] *_a_r_g_1, ... *_a_r_g_N; ! 14: ! 15: fscanf reads the file stream pointed to by fp, and uses the ! 16: string format to format the arguments arg1 through argN, each of ! 17: which must point to a variable of the appropriate data type. ! 18: ! 19: fscanf returns either the number of arguments matched, or EOF if ! 20: no arguments matched. ! 21: ! 22: For more information on fscanf's conversion codes, see scanf. ! 23: ! 24: ***** Example ***** ! 25: ! 26: The following example uses ffpprriinnttff to write some data into a ! 27: file, and then reads it back using ffssccaannff. ! 28: ! 29: ! 30: #include <stdio.h> ! 31: ! 32: main () ! 33: { ! 34: FILE *fp; ! 35: char let[4]; ! 36: ! 37: ! 38: ! 39: /* open file into write/read mode */ ! 40: if ((fp = fopen("tmpfile", "wr")) == NULL) { ! 41: printf("Cannot open 'tmpfile'\n"); ! 42: exit(1); ! 43: } ! 44: ! 45: ! 46: ! 47: /* write a string of chars into file */ ! 48: fprintf(fp, "1234"); ! 49: ! 50: ! 51: ! 52: /* move file pointer back to beginning of file */ ! 53: rewind(fp); ! 54: ! 55: ! 56: ! 57: /* read and print data from file */ ! 58: fscanf(fp, "%c %c %c %c", ! 59: &let[0], &let[1], &let[2], &let[3]); ! 60: printf("%c %c %c %c\n", ! 61: let[3], let[2], let[1], let[0]); ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: fscanf() STDIO fscanf() ! 70: ! 71: ! 72: ! 73: } ! 74: ! 75: ! 76: ***** See Also ***** ! 77: ! 78: scanf(), sscanf(), STDIO ! 79: ! 80: ***** Notes ***** ! 81: ! 82: Because C does not perform type checking, it is essential that an ! 83: argument match its specification. For that reason, fscanf is ! 84: best used only to process data that you are certain are in the ! 85: correct data format, such as data previously written out with ! 86: fprintf. ! 87: ! 88: ! 89: ! 90: ! 91: ! 92: ! 93: ! 94: ! 95: ! 96: ! 97: ! 98: ! 99: ! 100: ! 101: ! 102: ! 103: ! 104: ! 105: ! 106: ! 107: ! 108: ! 109: ! 110: ! 111: ! 112: ! 113: ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.