FILE INPUT / OUTPUT
Dong-Chul Kim BioMeCIS CSE @ UTA
7/21/2014
1
FILE INPUT / OUTPUT Dong-Chul Kim BioMeCIS CSE @ UTA Whats a file? - - PowerPoint PPT Presentation
7/21/2014 1 FILE INPUT / OUTPUT Dong-Chul Kim BioMeCIS CSE @ UTA Whats a file? A named section of storage, usually on a disk In C, a file is a continuous sequence of bytes Examples for the demand of a file in C programming:
Dong-Chul Kim BioMeCIS CSE @ UTA
7/21/2014
1
standard input, standard output, and the standard error output.
keyboard
for your system, e.g., display screen
character ('\n').
str to signal the end of the C string.
be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.
(gets(str)) == print out str “
returned.
#include <stdio.h> int main(void) { char mych[41]; //printf("Please intput a string with characters less than 40:\n"); puts("Please intput a string with characters less than 40:\n"); gets(mych); puts(mych); return 0; }
However, scanf(“%s”, mych) will only read the characters until the first space as one string.
pointer to a file
NULL is returned:
mode Function
"r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
#include <stdio.h> #include <stdlib.h> int main(void) { char fn[20]="";/*Without "", you will encounter an error on VS 2008, when you call strcat(). This is because fn is not a string without the initialization by “” – a string has to have the null character ‘\0’. */ char prefn[] = "myfile"; char exfn[] = ".txt"; char index[5];/*ranging from a to z*/ FILE * fp; scanf("%s",index); strcat(fn, prefn); printf("%s\n",fn); strcat(fn,index); printf("%s\n",fn); strcat(fn, exfn); printf("%s\n",fn); fp = fopen(fn, "w"); fclose(fp); return 0; }
A non-zero value is returned in the case that the End-of-File indicator associated with the stream is set. Otherwise, a zero value is returned.
#include <stdio.h> #include <stdlib.h> int main(void) { int ch; FILE * fp; fp = fopen("myfile.txt","r"); ch = getc(fp); while(ch != EOF) { putchar(ch); ch = getc(fp); } return 0; }
until
valid character and therefore it is included in the string copied to str.
read to signal the end of the C string.
read, the contents of str remain unchanged and a null pointer is returned.
#include <stdio.h> #include <stdlib.h> int main(void) { char fn[20]="myfile21.txt"; char buffer[100] = "";/*Note!!!*/ FILE * fp = fopen(fn, "r"); fgets(buffer, 50, fp);
printf("The number of characters in the file is %d.\n", strlen(buffer));
fclose(fp); return 0; }
any open files.
normally, and a non-zero value for abnormal termination
pass flow control to the previous level of recursion until the original level is reached. exit() still terminates the program.
On failure, EOF is returned.
#include <stdio.h> #include <stdlib.h> int main(void) { int ch; FILE * fp; long count = 0; fp = fopen("myfile.txt", "w");/*"r"*/ //fp = fopen("c:\\myfile.txt", "w"); /*By default, the file will be created in the project folder.*/ if (fp == NULL) { printf("Can't open myfile.txt.\n"); exit(1); /*exit()*/ } ch = getc(fp); /*getc*/ while (ch != EOF) /*EOF*/ { putc(ch, stdout); /*putc()*/ count++; } fclose(fp); /*fclose()*/ printf("File myfile.txt has %d characters\n", count); return 0; }