0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Cでテキストファイルの入出力

Last updated at Posted at 2021-02-25

標準ライブラリstdioを用いたファイル操作のまとめです.

#書き込み

write.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
   FILE *fp;
   fp = fopen("hoge.txt", "w");
   if (fp == NULL) {
      printf("error\n");
      exit(1);
   }
   fprintf(fp, "hello.\n");
   fclose(fp);
   return 0;
}


#読み込み

read.c
#include <stdio.h>
#include <stdlib.h>
int main(void){
   FILE *fp;
   int N=256;
   char str[N];
   fp = fopen("hoge.txt", "r");
   if (fp == NULL) {
      printf("error\n");
      exit(1);
   }
   while(fgets(str, N, fp) != NULL) {
      printf("%s\n", str);
   }
   fclose(fp);
   return 0;
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?