C语言01

结构体指针:
 

#include
struct address
{
	char name[30];
	char street[40];
	unsigned long tel;
	unsigned long zip;
};
int main()
{
	struct  address A[3] =
	{
		{"ZHANG","road1",1111,7777},
		{"lihua","road2",2222,8888},
		{"huhang","road3",3333,6666}
	};
	struct address* p;
	p = &A[0];
	printf("%s %s %u %u\n", p->name, p->street, p->tel, p->zip);
	return 0;
}

枚举:

输入一个数字0~6,输出对应的星期,比如输入0,输出Sunday,1~6分别是周一到周六

#include 

int main(){

    enum week{ Sun=0,Mon,Tues,Wed,Thurs,Fri,Sat} day;

    scanf("%d", &day);

    switch(day){

        case Mon: puts("Monday"); break;

        case Tues: puts("Tuesday"); break;

        case Wed: puts("Wednesday"); break;

        case Thurs: puts("Thursday"); break;

        case Fri: puts("Friday"); break;

        case Sat: puts("Saturday"); break;

        case Sun: puts("Sunday"); break;

        default: puts("input error!");

    }

    return 0;

}

读取文件:

#include
#include

int main()
{
	FILE* fp;
	char str[100];
	errno_t err;
	// 判断文件是否打开失败
	if ((err = fopen_s(&fp,"D:\\demo.txt", "rt")) != 0) {
		puts("文件打开失败");
		exit(0);
	}
	// 循环读取文件的每一行数据
	while (fgets(str, 100, fp) != NULL) {
		printf("%s", str);
	}
	// 关闭文件
	fclose(fp);
	return 0;

}

写入文件:

#include 
#include 
int main()
{
	FILE *fp;
	char ch;
	errno_t err;
	if ((err = fopen_s(&fp, "D:\\demo.txt", "wt+") != 0)) {
		puts("打开文件失败");
		exit(0);
	}

	while ((ch = getchar() != '\n')) {
		fprintf(fp,&ch);
	}
	fclose(fp);
	return 0;
}

C语言01_第1张图片

你可能感兴趣的:(c++,c语言,开发语言)