C语言读取文件内容统计代码

《C Primer PLus》第五版上的例子,亲自输入了一下。 功能就是读取一个文件的内容,然后统计字符。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INSERT 1
#define QUERY  2
#define UPDATE 3
#define DELETE 4
#define LIST   5
#define QUIT   0 

FILE *fp;

typedef struct
{
	char name[100];		//students' name
	char addr[100];		//students' address
	char phone[100];	//students' telephone
	char workfor[100];	//work unit
} TX;

TX txl[100];	// An array of alumni record, Up to one hundred students
int last = 0;	//The current number of alumni

void show_menu();
void do_choice(int);
int get_choice();
void do_quit();
void do_insert();
void do_update();
void do_query();
void do_delete();
void do_list_all();

void init();

int main(int argc, const char *argv[])
{
	int choice;

	init();
	while (1)
	{
		//1.display the main menu
		show_menu();
		//2.retrieve the user opition
		choice = get_choice();
		//3.According to the function of the corresponding choice of implementation
		do_choice(choice);
		//Sleep 5 sec, let the user see clearly
		sleep(3);	
		//	system("pause>nul");
	}
	return 0;
}

/* Increase initial classmates information*/

void init()
{
	int i = 0;

	if ((fp = fopen("tx1.txt", "rb")) == NULL)
	{
		//printf("can't open alumni information tx1.txt, please check on !\n");
		//exit(0);
		last = 0;
	}

	else
	{
		while (!feof(fp))		
		{
			if (fread(&txl[i], sizeof(TX), 1, fp) == 1)
			{
				//printf("read the first %d student\n", i);
				//sleep(5);
				i++;
			}
		}
		last = i;
	}

	fclose(fp);
}

void save()
{
	int i = 0;

	if ((fp = fopen("tx1.txt
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值