1 / 8

ファイル操作に ついて (1)

ファイル操作に ついて (1). ファイルへの書き込み. FILE* fp = fopen (&quot; output.html&quot;,&quot;w &quot;); if( fp == NULL){ printf (&quot; ファイルオープン失敗 &quot;); return 0; } fprintf ( fp ,&quot; 書き込む文字 <br>&quot;); int value = 1024; fprintf ( fp ,&quot;%d <br>&quot;, value); fclose ( fp );. ファイルからの読み込み ( fscanf ).

Download Presentation

ファイル操作に ついて (1)

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. ファイル操作について (1)

  2. ファイルへの書き込み FILE* fp = fopen("output.html","w"); if(fp == NULL){ printf("ファイルオープン失敗");return 0; } fprintf(fp,"書き込む文字\n"); int value = 1024; fprintf(fp,"%d \n", value); fclose(fp);

  3. ファイルからの読み込み (fscanf) FILE* fp = fopen("input.txt","r"); if(fp == NULL){ printf("ファイルオープン失敗");return 0; } int value; while(EOF!=fscanf(fp,"%d",&value)){ printf("読み込んだ数字 %d \n",value); } fclose(fp);

  4. ファイルからの読み込み (1行読む) FILE* fp = fopen("input.txt","r"); if(fp == NULL){ printf("ファイルオープン失敗");return 0; } char buf[1024]; while(NULL!=fgets(buf,sizeof(buf),fp)){ printf("読み込んだ文字 %s \n",buf); } fclose(fp);

  5. 文字列の部分比較について

  6. 文字列の部分比較 (strncmp) char str1[1024] = "12345"; char str2[1024] = "12388"; int ret = strncmp(str1,str2,3); printf("%d\n",ret); // 0と表示 ret = strncmp(str1,str2,4); printf("%d\n",ret); // -1と表示 // 辞書順でstr1<str2

  7. 文字列の部分表示について

  8. printfの%sについて char str[1024] = "12345"; printf("%s\n",str); // 12345と表示 // 文字の最初から\0まで printf("%s\n",&str[0]); // 上と同じ printf("%s\n",&str[2]); // 345と表示 // 3文字目から\0まで

More Related