练习13-1
#include <stdio.h>
int main(void)
{
FILE *fp;
char file_name[128];
printf("file name:");
scanf("%s",file_name);
fp = fopen(file_name, "r");//相对路径是跟源代码一样的路径
if (fp == NULL)
printf("\a无法打开文件\"%s\"。\n",file_name);
else {
printf("\a成功打开了文件\"%s\"。\n",file_name);
fclose(fp);
}
return 0;
}
练习13-2
#include <stdio.h>
int main(void)
{
FILE *fp;
char file_name[128];
printf("file name:");
scanf("%s",file_name);
fp = fopen(file_name, "w");//相对路径是跟源代码一样的路径
if (fp == NULL)
printf("\a无法打开文件\"%s\"。\n",file_name);
else {
printf("\a成功打开了文件\"%s\"。\n",file_name);
fclose(fp);
}
return 0;
}
练习13-3
#include <stdio.h>
#define NUM 6
typedef struct
{
char name[100];
double height;
double weight;
}Student;
void swap_Student(Student *x, Student *y)
{
Student temp = *x;
*x = *y;
*y = temp;
}
void sort_by_height(Student a[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = n - 1; j > i; j--)
if (a[j - 1].height > a[j].height)
{
swap_Student(&a[j - 1], &a[j]);
}
}
}
int main(void)
{
FILE *fp;
int ninzu = 0;
double hsum = 0.0;
double wsum = 0.0;
Student std[NUM];
if ((fp = fopen("hw.dat", "r")) == NULL)
printf("\a文件打开失败。\n");
else
{
while (fscanf(fp, "%s%lf%lf", std[ninzu].name, &(std[ninzu].height), &(std[ninzu].weight)) == 3)
{
hsum += std[ninzu].height;
wsum += std[ninzu].weight;
ninzu++;
}
sort_by_height(std, NUM);
for (ninzu = 0; ninzu<NUM; ninzu++)
printf("%-10s %5.1f %5.1f\n", std[ninzu].name, std[ninzu].height, std[ninzu].weight);
printf("----------------------\n");
printf("平均 %5.1f %5.1f\n", hsum / ninzu, wsum / ninzu);
fclose(fp);
}
return 0;
}
练习13-4
#include <stdio.h>
int main(void)
{
FILE *fp;
char name[100];
double height, weight;
fp = fopen("read_from_keyboard.txt","w");//file created in the same path as source code path
puts("input name(char[]) height(double) weight(double)");
puts("input invalid value to end input");
while (scanf("%s%lf%lf", name, &height, &weight) == 3)
{
fprintf(fp,"%-10s %5.1f %5.1f\n", name, height, weight);
}
fclose(fp);
printf("done!");
return 0;
}
练习13-5
#include <time.h>
#include <stdio.h>
char data_file[] = "datetime.dat";
char kimochi[128];
void get_data(void)
{
FILE *fp;
if ((fp = fopen(data_file, "r")) == NULL)
{
printf("本程序第一次运行。\n");
printf("当前的心情:");
scanf("%s",kimochi);
}
else
{
int year, month, day, h, m, s;
fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
printf("上一次运行是在%d年%d月%d日%d时%d分%d秒。\n",year, month, day, h, m, s);
fscanf(fp, "%s", kimochi);
printf("上一次心情:%s",kimochi);
printf("当前的心情:");
scanf("%s", kimochi);
fclose(fp);
}
}
void put_data(void)
{
FILE *fp;
time_t current = time(NULL);/* 当前日历时间 */
struct tm *timer = localtime(¤t);/* 分解时间*/
if ((fp = fopen(data_file, "w")) == NULL)
printf("\a文件打开失败。\n");
else {
fprintf(fp, "%d %d %d %d %d %d\n",
timer->tm_year + 1900, timer->tm_mon + 1, timer->tm_mday,
timer->tm_hour, timer->tm_min, timer->tm_sec);
fprintf(fp,"%s",kimochi);
fclose(fp);
}
}
int main(void)
{
get_data();/* 取得并显示上一次运行时的日期和时间 */
put_data();/* 写入本次运行时的日期和时间 */
return 0;
}
练习13-6
#include <stdio.h>
char file_name[128];
int main(void)
{
int ch;
int line_num = 0;
FILE *fp;
printf("input filename:");
scanf("%s",file_name);
if ((fp = fopen(file_name, "r")) == NULL)
{
printf("文件打开失败。\n");
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
line_num++;
}
printf("file has line number:%d",line_num);
}
return 0;
}
练习13-7
#include <stdio.h>
char file_name[128];
int main(void)
{
int ch;
int ch_num = 0;
FILE *fp;
printf("input filename:");
scanf("%s",file_name);
if ((fp = fopen(file_name, "r")) == NULL)
{
printf("文件打开失败。\n");
}
else
{
while ((ch = fgetc(fp)) != EOF)
{
if(ch!='\n')
ch_num++;
}
printf("file has character number:%d",ch_num);
}
return 0;
}
练习13-8
#include <stdio.h>
int main(void)
{
int ch;
FILE *sfp;/* 原文件 */
FILE *dfp;/* 目标文件 */
char sname[FILENAME_MAX];/* 原文件名 */
char dname[FILENAME_MAX];/* 目标文件名 */
printf("打开原文件:"); scanf("%s", sname);
printf("打开目标文件:"); scanf("%s", dname);
if ((sfp = fopen(sname, "r")) == NULL)
printf("\a原文件打开失败。\n");
else {
if ((dfp = fopen(dname, "w")) == NULL)
printf("\a目标文件打开失败。\n");
else {
while ((ch = fgetc(sfp)) != EOF)
{
fputc(ch, dfp);//输出到目标文件
printf("%c",ch);
}
fclose(dfp);
}
fclose(sfp);
}
return 0;
}
练习13-9
#include <stdio.h>
int main(void)
{
int ch;
FILE *sfp;/* 原文件 */
FILE *dfp;/* 目标文件 */
char sname[FILENAME_MAX];/* 原文件名 */
char dname[FILENAME_MAX];/* 目标文件名 */
printf("打开原文件:"); scanf("%s", sname);
printf("打开目标文件:"); scanf("%s", dname);
if ((sfp = fopen(sname, "r")) == NULL)
printf("\a原文件打开失败。\n");
else {
if ((dfp = fopen(dname, "w")) == NULL)
printf("\a目标文件打开失败。\n");
else {
while ((ch = fgetc(sfp)) != EOF)
{
if ((ch >= 'a') && (ch <= 'z'))
ch -= 32;
fputc(ch, dfp);//输出到目标文件
printf("%c",ch);
}
fclose(dfp);
}
fclose(sfp);
}
return 0;
}
练习13-10
#include <stdio.h>
int main(void)
{
int ch;
FILE *sfp;/* 原文件 */
FILE *dfp;/* 目标文件 */
char sname[FILENAME_MAX];/* 原文件名 */
char dname[FILENAME_MAX];/* 目标文件名 */
printf("打开原文件:"); scanf("%s", sname);
printf("打开目标文件:"); scanf("%s", dname);
if ((sfp = fopen(sname, "r")) == NULL)
printf("\a原文件打开失败。\n");
else {
if ((dfp = fopen(dname, "w")) == NULL)
printf("\a目标文件打开失败。\n");
else {
while ((ch = fgetc(sfp)) != EOF)
{
if ((ch >= 'A') && (ch <= 'Z'))
ch += 32;
fputc(ch, dfp);//输出到目标文件
printf("%c",ch);
}
fclose(dfp);
}
fclose(sfp);
}
return 0;
}
练习13-11
#include <stdio.h>
int main(void)
{
FILE *fp;
double haha[10] = {1.1,
2.2,
3.3,
4.4,
5.5,
6.6,
7.7,
8.8,
9.9,
10.10};
int i;
for(i=0;i<10;i++)
{
printf("从数组haha得到的元素haha[%d]为%23.21f。\n", i,haha[i]);
}
return 0;
}
练习13-12
#include <time.h>
#include <stdio.h>
char data_file[] = "datetime.dat";
void get_data(void)
{
FILE *fp;
if ((fp = fopen(data_file, "r")) == NULL)
printf("本程序第一次运行。\n");
else {
int year, month, day, h, m, s;
fscanf(fp, "%d%d%d%d%d%d", &year, &month, &day, &h, &m, &s);
printf("上一次运行是在%d年%d月%d日%d时%d分%d秒。\n",
year, month, day, h, m, s);
fclose(fp);
}
}
/*--- 写入本次运行时的日期和时间 ---*/
void put_data(void)
{
FILE *fp;
time_t current = time(NULL); /* 当前日历时间 */
struct tm *timer = localtime(¤t); /* 分解时间*/
if ((fp = fopen(data_file, "w")) == NULL) /* 打开文件 */
{
printf("\a文件打开失败。\n");
}
else
{
fwrite(&timer, sizeof(struct tm), 1, fp);//以二进制格式写入
fclose(fp);
}
}
int main(void)
{
get_data();/* 取得并显示上一次运行时的日期和时间 */
put_data();/* 写入本次运行时的日期和时间 */
return 0;
}
练习13-13
#include <stdio.h>
int main(void)
{
int n;
int ch;
FILE *sfp; /* 原文件 */
FILE *dfp; /* 目标文件 */
char sname[FILENAME_MAX]; /* 原文件名 */
char dname[FILENAME_MAX]; /* 目标文件名 */
printf("打开原文件:"); scanf("%s", sname);
printf("打开目标文件:"); scanf("%s", dname);
if ((sfp = fopen(sname, "rb")) == NULL)
printf("\a原文件打开失败。\n");
else
{
if ((dfp = fopen(dname, "wb")) == NULL)
printf("\a目标文件打开失败。\n");
else
{
while (n = fread(&ch, sizeof(int), 1, sfp) > 0)
{
fwrite(&ch, sizeof(int), 1, dfp);
}
fclose(dfp);
}
fclose(sfp);
printf("done!\n");
}
return 0;
}
hw.dat
Aiba 160 59.3
Kurata 162 51.6
Masaki 182 76.5
Tanaka 170 60.7
Tsuji 175 83.9
Washio 175 72.5