File IO fopen p FILE fopen const char
- Slides: 30
File I/O
fopen() p FILE* fopen( const char* filename, const char* mode ) n The fopen() function opens a file indicated by filename and returns a stream associated with that file. mode is used to determine how the file will be treated (i. e. for input, output, etc). 2
fclose() p int fclose ( FILE* stream ) n n The function fclose() closes the given file stream, deallocating any buffers associated with that stream. fclose() returns 0 upon success, and EOF otherwise. 3
fputs() p int fputs ( const char* str, FILE* stream ) n n The fputs() function writes a string str to the given output stream. The return value is non-negative on success, and EOF on failure. 4
Example: fputs() #include <iostream> If you run the program from Visual C++, it will be created in the same directory as your CPP file. int main() { FILE* p. File; p. File = fopen("myfile. txt", "w"); if (p. File != NULL) { fputs("fopen successfullyn", p. File); fputs("Output by fputs(). n", p. File); fclose(p. File); } return 0; } 5
Inspect the text file p In the command window, n Change directory to the working directory p n n For example, if your EXE file is "D: Visual. Studeio 2010ProjectstestDebugtest. exe", then run "test. exe" to generate the file "myfile. txt" in "D: Visual. Studeio 2010ProjectstestDebug" Inspect the content by “type myfile. txt” Edit the file by “notepad myfile. txt” 6
filename of fopen() p FILE* fopen( const char* filename, const char* mode ) n If you provide a simple filename like "myfile. txt", it will refer to a file in the same directory as your CPP file. n You may specify an absolute path name like "D: \myfile. txt" p Remember to use double backslashes to escape the character. 7
fprintf() and printf() int printf(const char * restrict format, . . . ); p int fprintf (FILE* stream, const char * format, . . . ) p n n n The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like printf() as far as the format goes. The return value of fprintf() is the number of characters outputted, or a negative number if an error occurs. 8
Example: fprintf () /* fprintf() example */ #include <iostream> int main () { FILE* p. File; p. File = fopen("myfile. txt", "w"); if (p. File == NULL) { printf("Cannot open file!n"); } else { for (int i=1; i<15; i+=3) fprintf (p. File, "Output: %02 dn", i); fclose (p. File); } return 0; } Output: Output: 01 04 07 10 13 9
Format of printf() p A format string, which consists of optional and required fields, has the following form: n p %[flags] [width] [. precision] type n determines the associated argument is interpreted as a character, a string, or a number (see the "printf Type Field Characters" table in printf Type Field Characters). p p p c – character s – string d – decimal x – hexadecimal o - octal http: //msdn. microsoft. com/en-us/library/56 e 442 dc. aspx 10
Format of printf() p flags n control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see the "Flag Characters" table in Flag Directives). More than one flag can appear in a format specification. p p 0 (zero-padding) - (left adjustment) + (always put a sign before a number) width n p (cont. ) specifies the minimum number of characters output (see printf Width Specification). precision n specifies the number of digits to appear after the decimal point, for e and f formats. p p printf("%5. 2 fn", 99. 99); printf("%05. 2 fn", 9. 9); 99. 99 09. 90 11
Example of Format Specification // Format of printf() #include <iostream> int main () { int i; char* a[] = { "Apple", "Banana", "Car" }; for (i=0; i< sizeof(a)/sizeof(a[0]); i++) printf("|%s|n", a[i]); printf("n"); for (i=0; i< sizeof(a)/sizeof(a[0]); i++) printf("|%10 s|n", a[i]); printf("n"); for (i=0; i< sizeof(a)/sizeof(a[0]); i++) printf("|%-10 s|n", a[i]); printf("n"); return 0; } |Apple| |Banana| |Car| | Apple| Banana| Car| |Apple |Banana |Car | | | 12
Example of fprintf() /* fprintf example */ #include <iostream> using std: : cout; using std: : cin; int main() { FILE * p. File; int n; char name[40]; p. File = fopen("myfile. txt", "w"); for (n=0 ; n<3 ; n++) { cout << "please, enter a name: "; cin >> name; fprintf (p. File, "Name %d [%-6 s]n", n, name); } fclose (p. File); return 0; } 13
fgets() p char * fgets ( char * str, int num, FILE * stream ); n n Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first. num - Maximum number of characters to be copied into str (including the terminating null-character). A terminating null ('