1 / 7

Understanding Function Usage and File Operations in C Programming

This guide explores the fundamentals of function definitions and file operations in C programming. The sections cover how to declare functions, handle parameters, and return values effectively. It also discusses using the `fopen`, `fscanf`, and `fprintf` functions to read from and write to files, demonstrating these concepts through practical code examples. Understanding this material requires a basic grasp of C syntax and file handling procedures, which are crucial for managing input and output in various applications.

yahto
Download Presentation

Understanding Function Usage and File Operations in C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Function and fopen(2)

  2. function • 語法 • 傳回值型態 函數名稱(參數傳遞) 參數傳遞型態 { 函數的主体 }

  3. function #include <stdio.h> main () { void example( ); example( ); } Void example( ) { printf(“ this is a function program!\n”); } Execute 完後回去

  4. #include <stdio.h> int sum (int x ,int y) { int z; : return (z); } void main(){ int a,b,c; : c=sum (a,b); : }

  5. Program-Controlled Input and Output Files • declare a file pointer variable • FILE *inp , /* pointer to input file */ • *outp ; /* pointer to output file */ • the calls to function fopen • inp = fopen(“b:distance.dat”, “r” ) ; • outp = fopen(“b:distance.out”, “w”) ; • use of the functions • fscanf(inp, “%lf”, &miles); • fprintf(outp, “The distance in miles is %.2f. \n”, miles); • end of use • fclose(inp); • fclose(outp);

  6. An example #include <stdio.h> int main(void) { int age; FILE *inp,*outp; inp=fopen("a1.dat","r"); outp=fopen("a2.out","w"); fscanf(inp,"%d",&age); fprintf(outp,"that is %d",age); fclose(inp); fclose(outp); } 必須要有a1.dat這一個檔案.來提供來源!~~ 從這讀進入 A1.dat 寫到這一個檔案 A2.out 必須關閉檔案

  7. ? • 有什麼問題?

More Related