Wednesday 25 July 2012

cstdio Library

<cstdio>



The stdio library is made mostly obsolete by the newer iostream library, but many programs still use it. There are facilities for random access files and greater control over output format, error handling, and temporary files. Mixing both I/O libraries is not recommended. There are no facilities for string I/O.

Global objects stdin, stdout, stderr of type FILE* correspond to cin, cout, cerr. s is type char*, c is char, n is int, f is FILE*.
FILE* f=fopen("filename", "r"); // Open for reading, NULL (0) if error
// Mode may also be "w" (write) "a" append, "a+" random access read/append,
// "rb", "wb", "ab", "a+b" are binary
fclose(f); // Close file f
fprintf(f, "x=%d", 3); // Print "x=3" Other conversions:
"%5d %u %-8ld" // int width 5, unsigned int, long left justified
"%o %x %X %lx" // octal, hex, HEX, long hex
"%f %5.1f" // double: 123.000000, 123.0
"%e %g" // 1.23e2, use either f or g
"%c %s" // char, char*
"%%" // %
sprintf(s, "x=%d", 3); // Print to array of char s
printf("x=%d", 3); // Print to stdout (screen unless redirected)
fprintf(stderr, ... // Print to standard error (not redirected)
getc(f); // Read one char (as an int, 0-255) or EOF (-1) from f
ungetc(c, f); // Put back one c to f
getchar(); // getc(stdin);
putc(c, f) // fprintf(f, "%c", c);
putchar(c); // putc(c, stdout);
fgets(s, n, f); // Read line including '\n' into char s[n] from f. NULL if EOF
gets(s) // fgets(s, INT_MAX, f); no '\n' or bounds check
fread(s, n, 1, f); // Read n bytes from f to s, return number read
fwrite(s, n, 1, f); // Write n bytes of s to f, return number written
fflush(f); // Force buffered writes to f
fseek(f, n, SEEK_SET); // Position binary file f at n
// or SEEK_CUR from current position, or SEEK_END from end
ftell(f); // Position in f, -1L if error
rewind(f); // fseek(f, 0L, SEEK_SET); clearerr(f);
feof(f); // Is f at end of file?
ferror(f); // Error in f?
perror(s); // Print char* s and last I/O error message to stderr
clearerr(f); // Clear error code for f
remove("filename"); // Delete file, return 0 if OK
rename("old", "new"); // Rename file, return 0 if OK
f = tmpfile(); // Create temporary file in mode "wb+"
tmpnam(s); // Put a unique file name in char s[L_tmpnam]

Example: input file name and print its size
char filename[100]; // Cannot be a string
printf("Enter filename\n"); // Prompt
gets(filename, 100, stdin); // Read line ending in "\n\0"
filename[strlen(filename)-1]=0; // Chop off '\n';
FILE* f=fopen(filename, "rb"); // Open for reading in binary mode
if (f) { // Open OK?
fseek(f, 0, SEEK_END); // Position at end
long n=ftell(f); // Get position
printf("%s has %ld bytes\n", filename, n);
fclose(f); // Or would close when program ends
}
else
perror(filename); // fprintf(stderr, "%s: not found\n", filename);
// or permission denied, etc.
printf(), fprintf(), and sprintf() accept a variable number of arguments, one for each "%" in the format string, which must be the appropriate type. The compiler does not check for this. printf("%d %f %s", 2, 2.0, "2"); // OK
printf("%s", 5); // Crash: expected a char* arg, read from address 5
printf("%s"); // Crash
printf("%s", string("hi")); // Crash: use "hi" or string("hi").c_str()

No comments:

Post a Comment