70 likes | 174 Views
Function and fopen(2). function. 語法 傳回值型態 函數名稱 ( 參數傳遞 ) 參數傳遞型態 { 函數的主体 }. function. #include <stdio.h> main () { void example( ); example( ); } Void example( ) { printf(“ this is a function program!<br>”); }. Execute 完後回去. #include <stdio.h> int sum (int x ,int y)
E N D
function • 語法 • 傳回值型態 函數名稱(參數傳遞) 參數傳遞型態 { 函數的主体 }
function #include <stdio.h> main () { void example( ); example( ); } Void example( ) { printf(“ this is a function program!\n”); } Execute 完後回去
#include <stdio.h> int sum (int x ,int y) { int z; : return (z); } void main(){ int a,b,c; : c=sum (a,b); : }
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);
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 必須關閉檔案
? • 有什麼問題?