C语言读取文件内容统计代码
2012-12-18 16:54
232 查看
《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", "wb")) == NULL) { //printf("can't open alumni information tx1.txt, please check on !\n"); //exit(0); } while (i < last) { fwrite(&txl[i], sizeof(TX), 1, fp); //printf("save the first %d student\n", i); //sleep(5); i++; } fclose(fp); } void show_menu() { system("clear"); printf("====Alumnni management information system\n"); printf("\t1. Increase students information\n"); printf("\t2. Find students information\n"); printf("\t3. Modify students information\n"); printf("\t4. Delete students information\n"); printf("\t5. List all students information\n"); printf("\t0. Quit system\n"); printf("=============================================\n"); printf("Please input your choice(0~5):\n"); } void do_quit() { printf("\n Welcome to use this system again, goodbye!\n"); exit(0); } void do_insert() { printf("deal with increseing......\n"); printf("name:"); scanf("%s", &txl[last].name); printf("address:"); scanf("%s", &txl[last].addr); printf("telephone:"); scanf("%s", &txl[last].phone); printf("Uint:"); scanf("%s", &txl[last].workfor); printf("Increase student success\n"); last++; // increase a student success , increased 1. save(); } void do_update() { char tname[100]; //Temporary storage input name. int i; int find = 0; //Whether the student find. printf("deal with modifying......\n"); printf("Please input to change the name of the students:"); scanf("%s", &tname); for (i = 0; i < last && !find; i++) { if (!strcmp(tname, txl[i].name)) { find = 1; printf("You find classmate information below:\n"); printf("name:%s\t", txl[i].name); printf("address:%s\t", txl[i].addr); printf("telephone:%s\t", txl[i].phone); printf("work unit:%s\t", txl[i].workfor); printf("\n Please modify:\n"); printf("name:"); scanf("%s", &txl[i].name); printf("address:"); scanf("%s", &txl[i].addr); printf("telephone:"); scanf("%s", &txl[i].phone); printf("Uint:"); scanf("%s", &txl[i].workfor); } } if (!find) { printf("I'm sorry, name for %s classmates haven't login in the alumni\n", tname); } else { printf("Name for %s classmates information has been modified successfully\n", tname); save(); } } void do_list_all() { int i; printf("The classmate alumni have %d student information:\n", last); for (i = 0; i < last; i++) { printf("===============================================================\n"); printf("name:%s\t", txl[i].name); printf("address:%s\t", txl[i].addr); printf("telephone:%s\t", txl[i].phone); printf("work unit:%s\t\n", txl[i].workfor); } printf("===============================================================\n"); } void do_query() { char tname[100]; //Temporary storage input name. int i; int find = 0; //Whether the student find. printf("deal with querying......\n"); printf("Please input to change the name of the students:"); scanf("%s", &tname); for (i = 0; i < last && !find; i++) { if (!strcmp(tname, txl[i].name)) { find = 1; printf("You find classmate information below:\n"); printf("name:%s\t", txl[i].name); printf("address:%s\t", txl[i].addr); printf("telephone:%s\t", txl[i].phone); printf("work unit:%s\t\n", txl[i].workfor); } } if (!find) { printf("I'm sorry, name for %s classmates haven't login in the alumni\n", tname); } } void do_delete() { char tname[100]; //Temporary storage to delete students' name. int i; int find = 0; //Whether the student find. int index; //Record to be deleted in students of the array index printf("deal with deleteing......\n"); printf("Please input to be deleted the name of the students:"); scanf("%s", &tname); for (i = 0; i < last && !find; i++) { if (!strcmp(tname, txl[i].name)) { find = 1; index = i; //Storage to delete classmate subcript position break; } } if (!find) { printf("I'm sorry, name for %s classmates haven't login in the alumni\n", tname); } else { for (i = index; i <= last; i++) { strcpy(txl[i].name, txl[i+1].name); strcpy(txl[i].addr, txl[i+1].addr); strcpy(txl[i].phone, txl[i+1].phone); strcpy(txl[i].workfor, txl[i+1].workfor); } last--; printf("Name for %s classmates information has been deleted successfully\n", tname); save(); } } int get_choice() { int choice; scanf("%d", &choice); return choice; } void do_choice(int choice) { printf("is in the process of the first %d a choice \n", choice); switch (choice) { case INSERT : do_insert(); break; case QUERY: do_query(); break; case UPDATE: do_update(); break; case DELETE: do_delete(); break; case LIST: do_list_all(); break; case QUIT: do_quit(); break; default: break; } }
#include <stdio.h> #include <stdlib.h> int main ( int argc, char * argv[] ) { int ch ; FILE * fp ; long count = 0 ; //检查argc是否为2 if( argc != 2 ) { printf(" Usage: %s Filename\n", argv[0]) ; exit(1) ; } //读取字符串(文件名),如果为NULL,则提示输出错误信息。 if( ( fp = fopen( argv[1], "r" ) ) == NULL ) { printf("Can't open %s\n",argv[1]) ; exit(2) ; } //如果不为NULL,一直读取到文件结尾,并输出读取到的内容。 while( ( ch = getc(fp) ) != EOF ) { putc(ch,stdout) ; count++ ; } //结束 fclose(fp) ; //输出总信息 printf("File %s has %ld characters\n", argv[1], count); return 0 ; }
相关文章推荐
- 一句代码读取properties文件内容
- 用c语言输入学生信息保存到文件中并读取文件内容
- java读取文件内容的三种方法代码片断分享(java文件操作)
- PHP 读取文件内容代码(txt,js等)
- Android 读取assets和raw文件内容实例代码
- 读取配置文件的内容代码
- C语言逐行读取文件内容,写入另外一个文件
- linux下c语言实现tail -f功能---实时读取变化文件中的增量内容
- 读取文件的目录结构和统计文件的代码信息
- C语言读取配置文件代码
- C语言一次性读取文件全部内容
- C语言读取文件内容
- PHP读取文件内容后清空文件示例代码
- PHP读取网页文件内容的实现代码(fopen,curl等)
- AB程序核心代码(MFC从文件读取一行+MFC字符串处理+C在文件末尾追加内容)
- 通过Javascript读取本地Excel文件内容的代码示例
- iOS开发代码:从文本文件中读取内容到字符串里(+ .json文件)
- C语言读取文件的内容
- Java读取文件内容并输出的最简单的方式之一(只需三行代码)
- C语言中把结构内容写入二进制文件及从二进制文件中读取出数据