1 / 13

小林 学

湘南工科大学. 2012 年 1 月 17 日. 情報理論2 第13回. 小林 学. 〒251-8511  神奈川県藤沢市辻堂西海岸 1-1-25. Tel. 0466-30-0232( 直通 ). Fax. 0466-34-5932. kobayasi@info.shonan-it.ac.jp. [ A201教室でファイルを読み書きするプログラム作成の注意 ] VisualStudio2010 「新しいプロジェクト」画面において,「参照」ボタンをクリックしてプロジェクトの保存「場所」を以下のように設定する.

Download Presentation

小林 学

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. 湘南工科大学 2012年1月17日 情報理論2 第13回 小林 学 〒251-8511 神奈川県藤沢市辻堂西海岸1-1-25 Tel. 0466-30-0232(直通) Fax. 0466-34-5932 kobayasi@info.shonan-it.ac.jp

  2. [A201教室でファイルを読み書きするプログラム作成の注意]VisualStudio2010「新しいプロジェクト」画面において,「参照」ボタンをクリックしてプロジェクトの保存「場所」を以下のように設定する[A201教室でファイルを読み書きするプログラム作成の注意]VisualStudio2010「新しいプロジェクト」画面において,「参照」ボタンをクリックしてプロジェクトの保存「場所」を以下のように設定する z:\document\MyDocument\VisualStudio 2010\Projects チェックはなくす

  3. [A201教室でファイルを読み書きするプログラム作成の注意]読み込むファイルを置くフォルダは以下とする[A201教室でファイルを読み書きするプログラム作成の注意]読み込むファイルを置くフォルダは以下とする z:\document\MyDocument\VisualStudio 2010\Projects\プロジェクト名 [例]testというプロジェクトを作成し,「demo.txt」というファイルを読み込むプログラムを作成したい場合 のフォルダに「demo.txt」を置く z:\document\MyDocument\VisualStudio 2010\Projects\test

  4. [ファイル入力1] #include<stdio.h> int main(void){ FILE *fp; char moji; fp = fopen("demo.txt","r"); while(1){ moji = fgetc(fp); if(moji == EOF) break; printf("%c",moji); } fclose(fp); return(0); } 開いたファイルを意味する変数 読み込むファイル名 r:読込モード ファイルから半角1文字が読込まれ、moji に入る ha

  5. 指定されたファイルを開く.fpは開いたファイルを意味する.指定されたファイルを開く.fpは開いたファイルを意味する. “モード”には r:読込モード(読込専用) w:書込モード(書込専用、上書き) a:追記モード(書込だが、追記) fp = fopen("ファイル名", "モード"); ファイルを閉じる.必ず必要. fclose(fp);

  6. [前回の課題1解答]ファイルdemo.txtから4文字読み込み,char型の配列strに入れなさい.(文字列の最後は0を入れる点に注意)[前回の課題1解答]ファイルdemo.txtから4文字読み込み,char型の配列strに入れなさい.(文字列の最後は0を入れる点に注意) #include<stdio.h> void main(void){ FILE *fp; char str[8]; int i; fp = fopen("demo.txt","r"); for(i=0;i<4;i++) str[i]=fgetc(fp); str[4]=0; fclose(fp); printf("str=%s", str); }

  7. [ファイル出力] #include<stdio.h> int main(void){ FILE *fp; fp=fopen("write.txt","w"); fprintf(fp,"今日の日付は%d月%d日です", 12, 2); fclose(fp); return(0); } 書込むファイル名 書込みモード printfと同様に書くと,ファイルに書き込まれる //printfのようにファイルに書込む命令 書込むファイルを指定

  8. 書込みモード 例題(foutput.c) #include<stdio.h> #include<math.h> void main(void){ FILE *fp; double x, y, z; fp = fopen("write.csv","w"); for(x=0; x < 2*3.14159; x = x + 0.01){ y = sin(x); z = cos(x); fprintf(fp,"%lf,%lf,%lf\n", x, y, z); } fclose(fp); } 書込むファイル名 printfと同様に書くと,ファイルに書き込まれる 書込むファイルを指定

  9. [課題2]架空の3人の氏名,住所,メールアドレスをwrite.csvファイルに書き出すプログラムを完成させなさい.[課題2]架空の3人の氏名,住所,メールアドレスをwrite.csvファイルに書き出すプログラムを完成させなさい. #include<stdio.h> void main(void){ FILE *fp; fp = fopen("write.csv", "w"); ??? //ここにプログラムを書く fclose(fp); }

  10. [課題3] 関数 y = x3-2x2+1 とする.x=-2,-1.9,...,1.9,2 それぞれに対し,x,y の値をそれぞれ write.csvファイルに書き込むプログラムを作成しなさい.またExcelで図を表示しなさい. #include<stdio.h> void main(void){ FILE *fp; double x, y; fp = fopen("write.csv", "w"); for(x=-2; x<2.1; x=x+0.1){ ??? //ここにプログラムを書く } fclose(fp); }

  11. [スペースで区切られたテキストファイルの読み込み方法][スペースで区切られたテキストファイルの読み込み方法] data.txtの中身 小林学 神奈川県藤沢市辻堂 kobayasi@info.shonan-it.ac.jp 40 二宮洋 神奈川県藤沢市鵠沼 ninomiya@info.shonan-it.ac.jp 42 data.txtを読み込むプログラム #include<stdio.h> void main(void){ char name[20], address[100], email[100]; FILE *fp; int i, age; fp=fopen("data.txt","r"); for(i=0;i<2;i++){ fscanf(fp, "%s %s %s %d",name, address,email, &age); printf("%s %s %s %d\n",name,address,email,age); } fclose(fp); }

  12. [課題4] 次のdata.txtファイルを読み込んで画面に表示するプログラムを完成させなさい.ただし読み込むデータの変数は左から char name[20], int nenrei, double taijuとしなさい. data.txtの中身 小林学 40 71.5 二宮洋 42 68.5 鈴木誠 44 65.3 #include<stdio.h> void main(void){ FILE *fp; char name[20]; int nenrei, i; double taiju; fp = fopen("data.txt", "r"); for(i=0;i<3;i++){ ??? //ここにプログラムを書く } fclose(fp); }

  13. [課題5] 課題3のx,yをスペース区切りでdata.txtにファイル出力し,さらにdata.txtの内容を読み込んで画面に表示するプログラムを作成しなさい. #include<stdio.h> void main(void){ FILE *fp; double x, y; int i; fp = fopen("data.txt", "w"); for(x=-2; x<2.1; x=x+0.1){ ??? //ここにプログラムを書く } fclose(fp); fp = fopen("data.txt", "r"); for(i=0;i<???;i++){ ??? //ここにプログラムを書く } fclose(fp); }

More Related