70 likes | 231 Views
File Processing. Read from and Write to Text Files. Using Input/Output Files. Declare file pointers FILE * inpf , /* pointer to a file for input */ * outpf ; /* pointer to a file for output */ Open files for reading and writing inpf = fopen (“data.txt“, “r“);
E N D
File Processing Read from and Write to Text Files
Using Input/Output Files • Declare file pointers FILE *inpf, /* pointer to a file for input */ *outpf; /* pointer to a file for output */ • Open files for reading and writing inpf = fopen(“data.txt“, “r“); outpf = fopen(“out.txt“, “w“); • Read from and Write into files fscanf(inpf, “%lf“, &Radius); fprintf(outpf, “Area of Circle is %f\n“, AreaCircle); • Close all open files fclose (inpf); fclose(outpf); CENG 114
fopen • fopen(arg1, arg2) • Input: arg1 is a string which is the name of the file to be opened. If the file is not in the same directory as the program, you should include the path as well (“C:\users\myname\desktop\projects.txt”) • Input: arg2 is a string as shown below: • “r” means read. • “w” means write • “a” means append • Output: Returns a pointer • If “r” is used, returns a pointer pointing to the first character of the file. If the file cannot be opened it returns NULL pointer. • If “w” is used, returns a pointer pointing to the beginning of the file. If the file does not exist, it creates the file; if it exists, it clears the contents. • If “a” is used, returns a pointer pointing to the end of the file. If the file does not exist, it creates the file.
fgets/fputs • fgets(char *str, int n, FILE *) • Input: A pointer to a string (char array), an integer for maximum characters to be read, and a file pointer • Output: Reads one line into this array until \n. It stores \n, and it puts \0 at end. This function never stores more than n-1 characters (plus \0). If the line is more than n-1 characters, then the first n-1 characters are read and \0 is put at the end (no \n is stored). • When fgets encounters EOF, it returns a NULL pointer. • fputs(char *str, FILE *) • Input: A pointer to a string array (or the string in “ “) and a file pointer • Output: Prints the string into file CENG 114
Example - fgets/fputs #include <stdio.h> #include <string.h> int main(void) { char line[80]; FILE *myfilep; char *status; myfilep = fopen("Myfile.txt", "w"); fputs("This is line 1\n", myfilep); fputs("This is a long long line which is line 2\n", myfilep); fputs("This is line 3", myfilep); fclose(myfilep); CENG 114
Example - fgets/fputs myfilep = fopen("Myfile.txt", "r"); status = fgets(line, 50, myfilep); while(status != 0) { if(line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = '\0'; puts(line); status = fgets(line, 50, myfilep); } return (0); } CENG 114
getchar/putchargetc/putc • getchar() • reads a character from standard input • returns the value of the character • ch = getchar() scanf(“%c”, &ch) • putchar(char ch) • prints the character argument onto standard output • putchar(ch) printf(“%c”, ch) • getc(FILE *inpfl) • Same as getchar, but reads from a file • ch = getc(inpfl) • putc(char, FILE *outpfl) • Same as putchar, but prints to a file • putc(ch, outpfl) CENG 114