MAN FILE fopen char Filename char Mode Filename
- Slides: 14
MAN
FILE *fopen (char *Filename, char *Mode); § Filename § Path of the file § Mode § How do you use the file? § Use header stdio. h § Returns a pointer to the opened file § If fails returns NULL
§ Read mode ("r") § if file is unavailable error § Write mode (“w") § if file is unavailable created § Overwrite § Append mode (“a") § if file is unavailable created
int fclose (FILE *fp); § In order to improve efficiency most file system write data to disk one sector at a time § fclose flushes the buffer
CHARACTER int fgetc (FILE *fp); § Similar to getchar() int fputc (int ch, FILE *fp); § Similar to putchar()
§ reading a text file and displaying it in the screen #include <stdio. h> int main(void) { FILE *fp; if ((fp = fopen ("a. txt", "r"))!=NULL){ while (! feof (fp)){ putchar (fgetc(fp)); } fclose (fp); } return 0; }
OF FILE int feof (FILE *fp); § Returns § non zero if fp reached end of file § Zero otherwise int ferror (FILE *fp); § Returns § non zero if error occured § Zero otherwise END
STRINGS/LINES int fputs (char *str, FILE *fp); int fgets (char *str, int num, FILE *fp); § Reads characters from the file associated with fp into string pointed by str until § num-1 characters have been read or § A newline character is found or § End of file
#include <stdio. h> int main(void) { FILE *fp; if ((fp = fopen ("a. txt", "w"))!=NULL){ fputs("Write test", fp); fclose (fp); } return 0; }
#include <stdio. h> int main(void) { FILE *fp; char str[80]; if ((fp = fopen ("a. txt", "w"))!=NULL){ fputs("Write test", fp); fclose (fp); } if ((fp = fopen ("a. txt", "r"))!=NULL){ fgets(str, 4, fp); fclose (fp); printf(str); } return 0; }
FORMATTED int fprintf (FILE *fp, format speci, variable(s)); § Just like printf. § Return § ON SUCCESS: the number of bytes output § ON FAILURE: returns EOF int fscanf (FILE *fp, format speci, address(es)); § Just like scanf. § Return § ON SUCCESS: Number of scanned inputs. § ON FAILURE: fscanf returns EOF
#include <stdio. h> int main(void) { FILE *fp; int n, x; scanf("%d", &n); if ((fp = fopen ("a. txt", "w"))!=NULL){ fprintf(fp, "%d", n); fclose (fp); } if ((fp = fopen ("a. txt", "r"))!=NULL){ fscanf(fp, "%d", &x); fclose (fp); printf("n%dn", x); } return 0; }
§ File handle : FILE * § Open a file : fopen § Input/Output § Character IO : getc, putc § Line/String IO : fgets, fputs § Formatted IO : fscanf, fprintf § Close a file : fclose
§ Dr. S. M. Farhad § Johra Muhammad Moosa
- Man vs man conflict
- Bmode
- File-file yang dibuat oleh user pada jenis file di linux
- For output as #1
- Char char slide
- Const char * vs char * const
- Readtext r
- Fopen sar
- Fopen msdn
- Msdn fopen
- Focus mode and diffuse mode
- Mode địa chỉ tức thì là mode
- Difference between real protected and virtual mode of 80386
- Timer in microcontroller
- Gartner mode 1 mode 2