1
3

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 5 years have passed since last update.

c言語

Last updated at Posted at 2019-01-24

C++ばかりでC言語がまともに書けないのはアレなので、
fopen
fopen関数はファイルを開きそのポインタを取得する関数です。
書式は
FILE *fopen(const char *filename,const char *mode);
モードには色々ある。rは読み込む専用
サンプルコード

# include <stdio.h>

int main(void)
{

	FILE *fp;
	char buf[50];

	fp = fopen("test.txt","r");

	fgets(buf,30,fp);

	puts(buf);

	fclose(fp);//fopenの戻り値として取得したファイルポインタを引数に渡してクローズしている。	
	return 0;
}

test.txtには
test,123,456という文字列が書き込まれているのでtestが表示される

char*filename,const char
エラー時はNULLが返ります。
fgets

char *fgets(char *s, int n, FILE *fp);

第一引数は文字配列のポインタ
第二引数は一行の最大文字数
第三引数はファイルポインタ

をそれぞれ指定する。
第二引数の最大文字数には末尾の\0も含まれるので、
実際に取得できる文字数は「最大文字数-1」になる。

サンプルコード

# include <stdio.h>

int main(void)
{

	FILE *fp;
	char buf[50];

	fp = fopen("test.txt","r");

	fgets(buf,30,fp);

	puts(buf);

	fclose(fp);
		
	return 0;
}

aiueoと入力するとaiueoと返ってくる

一年の各月の日数を表示するプログラム

# include<stdio.h>
# include<stdlib.h>
int main() {
	int seireki, nissu;
	char line[20];
	printf("西暦を入力:");
	fgets(line, sizeof(line), stdin);
	seireki = atoi(line);
	if (seireki % 4 == 0) {
		printf("西暦%d年はうるう年です\n", seireki); 
	}
	else {
		printf("西暦%d年はうるう年ではありません\n", seireki);
	}
	//キーボードから入力した西暦の文字列を整数に変換して変数seirekiに代入し画面に表示
	for (int tuki = 1; tuki <= 12; ++tuki) {
		nissu = get_days_of_month(tuki, seireki);
		printf("%2d月は%3d日あります。\n", tuki, nissu);
	}

	return 0;
}

int get_days_of_month(int month, int year) {
	int days[] = {
		0,31,28,31,30,31,30,31,31,30,31,30,31
	};
	if (year % 4 == 0)days[2]++;
	return days[month];
}

名前を入力して画面表示する

# include<stdio.h>
# include<string.h>
int main() {
	char name[30];
	while (1) {
		printf("あなたの名前は?");
		fgets(name, sizeof(name), stdin);
		name[strlen(name) - 1] = '\0';//改行文字を取り除く
		if (strlen(name) >= 1) {
			printf("あなたは%sさんです\n", name);
		}
		else {
			printf("恥ずかしがり屋さんね\n");
			break;
		}
	}

	return 0;
}
# include<iostream>
# include<vector>
# include<algorithm>
# include<string>
# include<map>
# include<math.h>
# include<queue>
# include<deque>
# include<stack>
# include<cstdio>
# include<utility>
# include<set>
# include<list>
# include<cmath>
# include<stdio.h>
# include<string.h>
# include<cstdio>
# include<iomanip>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int INF = 1e9 + 7;
//名前を入力して場所とともに表示するプログラム
int main() {
	char basho[11];
	char sakusha[16], riyousha[16];
	strcpy_s(basho, "早稲田大学");
	strncpy_s(sakusha, "terunobu",sizeof(sakusha)-1);
	sakusha[sizeof(sakusha)-1] = '\n';
	printf("利用者の名前は?");
	fgets(riyousha, sizeof(riyousha), stdin);
	riyousha[strlen(riyousha) - 1] = '\0';
	printf("ここは%sです\n", basho);
	if (strcmp(riyousha, sakusha) == 0) {
		printf("あなたはこのプログラムを作った%sさんです\n", riyousha);
	}
	return 0;
}
1
3
0

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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?