National Taiwan University Department of Computer Science and

  • Slides: 8
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Manipulating File IO in

National Taiwan University Department of Computer Science and Information Engineering Manipulating File IO in Visual C++ Speaker: Yao-Ting Huang Advisor: Kun-Mao Chao National Taiwan University Department of Computer Science & Information Engineering Algorithms and Computational Biology Lab. 2021/6/11

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions n n You have to include <stdio. h>. You have to use the file pointer. #include <stdio. h> File *fp; fp = fopen(“myfile. txt”, “w”); fprintf(fp, ”Hello!”); fclose(fp); n The fopen function requests OS for a memory buffer. u Your program actually reads/writes to the buffer, and the OS controls the trasmission between the buffer and disk. 2

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions n The fclose function flushes the remaining contents in the buffer and release the pointer and buffer. u The maximum number of opened file pointers are depending on OS. Write C Program Read Memory Buffer Write Disk Read 3

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions

National Taiwan University Department of Computer Science and Information Engineering Standard IO Manipulating Functions n The syntax of fopen File* fopen(char *filename, char *mode); u mode: “r”, “w”, “a”, “r+”, “w+b”, …etc. u If the file can not be open or created, the fopen function returns 0 (NULL). u n Other file manipulating functions: fscanf and fprintf: formatted input/output file functions. u fgetc, fputc: get or put a single char from or into a file. u fgets, fputs: get or out a string from or into a file. u n See an example. 4

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (1/3) n

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (1/3) n n n Write a program that prompts the user to input an integer n. Draw a triangle with n levels, and each level has n star symbols. For example, n = 3, * ** *** After drawing the triangle, repeat the above process until the user inputs a negative integer. 5

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (2/3) n

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (2/3) n An usage scenario: Please input: 2 * ** Please input: 3 * ** *** Please input: -9 Thank you for using this program. 6

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (3/3) n

National Taiwan University Department of Computer Science and Information Engineering Homework #4 (3/3) n n You can draw other shapes to get bonus. e. g. , n=3 (triangle) * * * e. g. , n=3 (reverse triangle) * * * Due date: 11/18. 7

National Taiwan University Department of Computer Science and Information Engineering Outline of Lecture Today

National Taiwan University Department of Computer Science and Information Engineering Outline of Lecture Today n n A C file application. Demo of your Homework #3. u Please make sure your program is ready to execute. 8