用C写果然最有成就感。。。不像用delphi,大部分是开发环境生成的,成就感少了一点。
c语言博大精深,千万不要觉得c++/Java更洋气一些。James Gosling现在就后悔当初Java引入了类,现在有多少人正在将Java当成OOP语言真正的在用呢?
c是除了汇编外最让人尊重的语言,老前辈,一定要牢牢掌握
用c写的是控制台下应用的一个小软件,提供主菜单,根据选择调用相应函数。命名规则采用unix环境下推荐的命名规则。只不过缩进我还是用空格敲,不喜欢用tab,虽然可能每次要敲4个或8个空格我还是敲,呵呵
------------------------------------------.c文件------------------------------------------------------
/*
* fileoperator.c
* Programme Name:Exercise
* Copyright@ Hundun Version: V 1.0
* Function:Provide main menu under console, invoke certain function according to user's choice
* Author:Youbin Wang
* Record of Modification:
* 2009.04.16 Add the Version commentary. Separate the .c file and .h file, add a function
* open_file_readonly() by Youbin Wang
* 2009.04.15 Create the main programme,
* declare and define Selection,ShowMenu,CreateFile,OpenFile,ExitSystem by Youbin Wang
*/
#include
#include
#include"user01_20090416.h"
/*
* Show main menu if user haven't decided to exit the system
* or they just finish one operation
* Input parameter: none
* Output Parameter: return 1 while execute successfully
*/
int show_menu()
{
printf("/*--------------------------------------------\n");
printf(" * Welcome TO This Programme \n");
printf(" * This is the Main Menu \n");
printf(" *--------------------------------------------*/\n");
printf("\n");
printf("Please Make your selection\n");
printf(" 1: Create a new file and write;\n");
printf(" 2: Open a file and write;\n");
printf(" 3: Open a file with read-only mode;\n");
printf(" 4: Exit;\n");
printf("\n");
return 1;
}
/*
* if the user choose to open a file with readonly mode(3) then invoke this function
* InputParameter:None
* OutputParameter: return 1 if execute successfully, return 0 if a error occured while open a file
*/
int open_file_readonly()
{
char c_output;
FILE *fl_p_readonly;
fl_p_readonly = fopen("mytext.txt","rt");
if(NULL==fl_p_readonly)
{
printf("\nCannot open file readonly, press any key to continue!\n");
getchar();
return 0;
}
printf("Successfully read a file\n");
sleep(1);
c_output=fgetc(fl_p_readonly);
while(c_output!=EOF)
{
printf("I'm reading the text\n");
sleep(1);
putchar(c_output);
c_output=fgetc(fl_p_readonly);
}
fclose(fl_p_readonly);
return 1;
}
/*
* if the user choose to create a new file(1) then invoke this function
* InputParameter: None
* OutputParameter: return 1 while execute successfully, o while error accured in creating file
*/
int create_file()
{
printf("Creating a file...\n");
FILE *fl_createfile;
fl_createfile = fopen("new.txt","wt+");
if(NULL==fl_createfile)
{
printf("\nCannot create file, press any key to continue!\n");
getchar();
return 0;
}
char c_inputchar;
printf("Successfully create a file!\n");
sleep(1);
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
printf("I'm reading your input\n");
fputc(c_inputchar,fl_createfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_createfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_createfile);
while(c_inputchar!=EOF)
{
printf("I'm writing your input\n");
putchar(c_inputchar);
c_inputchar=fgetc(fl_createfile);
}
printf("\n");
fclose(fl_createfile);
printf("Done!!\n");
return 1;
}
/*
* if the user choose to open a exist file(2) and write then invoke this function
* InputParameter:None
* OutputParameter: return 1 while execute successfully, 0 when a error occured in creating or read file
*/
int open_file()
{
printf("Opening a file...\n");
FILE *fl_openfile;
fl_openfile = fopen("mytext.txt","wt+");
if(NULL==fl_openfile)
{
printf("\nCannot open file, press any key to continue!\n");
getchar();
return 0;
}
char c_inputchar;
printf("Successfully open a file");
printf("Please input your text: \n");
while (c_inputchar!='\n')
{
fputc(c_inputchar,fl_openfile);
c_inputchar=getchar();
}
/* rewind the position of file pointer to the top of file */
rewind(fl_openfile);
printf("Your inputment is: ");
c_inputchar=fgetc(fl_openfile);
while(c_inputchar!=EOF)
{
putchar(c_inputchar);
c_inputchar=fgetc(fl_openfile);
}
printf("\n");
fclose(fl_openfile);
printf("Done!!\n");
return 1;
}
/*
* if the user choose to exit the system then invoke this function
* InputParameter:None
* OutputParameter: retrun 1 while execute successfully
*/
int exit_system()
{
printf("\n");
printf("Exiting.........\n");
printf("\n");
printf(" Thank you for using this system.\n");
printf("\n");
printf(" If you have any problems or advices,contract the author at qisi1986@gmail.com\n");
printf("\n");
printf(" Wish you a good day. Bye Bye.\n");
return 1;
}
/*
* if the user input the selection, then the function 'main' will invoke this function
* InputParameter: the selection of user
* OutputParameter: return 1 while successfully
* if the user put the wrong key then it will provide hints
*/
int Selection(int Index)
{
printf("Your Selection is %d \n", Index);
printf("Now will invoke corresponding function,please wait.");
printf("\n");
sleep(1);
switch(Index)
{
case 1:
create_file();
break;
case 2:
open_file();
break;
case 3:
open_file_readonly();
break;
case 4:
exit_system();
break;
default:
printf("Error!Please Enter a correct number!\n");
sleep(1);
}
return 1;
}
int main()
{
char inputInt;
system("clear");
while(inputInt != 4)
{
system("clear");
show_menu();
printf("Enter your selection here: ");
scanf("%d", &inputInt);
Selection(inputInt);
}
return 1;
}
------------------------------------------------.h文件------------------------------------------
/*
* fileoperator.h
* Copyright@ Hundun Version: V 1.0
* Function:contains all the declaration of functions in fileoperator.c
* Author: Youbin Wang
* Record of Modification:
* 2009.04.16 Create the .h File, add open_file_readonly by Youbin Wang
*/
#include
/*
* The declaration of functions
* CreateFile,OpenFile, ExitSystem are invoked by Selection
*/
int show_menu();
int create_file();
int open_file();
int exit_system();
int selection(int index);
int open_file_readonly();
No comments:
Post a Comment