Programming in C language: editing the word book

catalog

summary

Edit the word function, including the functions of adding, modifying, deleting and supplementing words. Therefore, you need to write a menu subfunction to select the function, and then write the corresponding subroutine. The specific code is as follows:

Menu functions

void Vocabulary()
{
	int i;
	do{
			printf("1.Add words\n");
	        printf("2.Find words\n");
	        printf("3.Delete word\n");
			printf("0.Back to main menu\n");
			printf("Please enter your choice:");
			scanf("%d",&i);
			getchar();
			switch(i)
			{
			case 1: 
				addword();  //Jump to the subfunction corresponding to the added word
				break;
			case 2:
				findword();//Jump to the subfunction corresponding to the search word
				break;
			case 3: delword();	//delword() is the corresponding subfunction of the deleted word
				break;
			case 0:
				system("cls");
				Menu();
				break;
			default:printf("Please enter the correct option!\n");
			}

	}while(i);
}

Delete word subfunction

void delword()
{
	int ch=0,count=0,count1=0;  //Define the number of integers
	char del[500];              //Define character array to store user input (to be deleted)
	typedef struct stu          //Define structure
	{
		int j;
		char px[500];
		char zw[500];
	}dcxx;
	dcxx dcsj[1000];            //Define an array of structures to store word data
	int i=0;
	FILE *fp1,*fp2;               //Define file pointer
	fp1=fopen("D:\\Ctext\\danciben.txt","r+");   //Two file pointers point to two files respectively
	fp2=fopen("D:\\Ctext\\danciben1.txt","w+");  //Open in the form of "w +", if there is no such file, it will be created automatically
	if(!fp1)                    //Judge whether the file is opened successfully
	{
		printf("file error\n");
		exit(1);
	};
	if(!fp2)
	{
		printf("file error\n");
		exit(1);
	};
	fscanf(fp1,"%d%s%s",&dcsj[i].j,dcsj[i].px,dcsj[i].zw);  //Read out the word data in the file and store it in the structure array
	while(!feof(fp1))
	{
		i++;
		fscanf(fp1,"%d%s%s",&dcsj[i].j,dcsj[i].px,dcsj[i].zw);
	}
	fclose(fp1);   //Close file
	count=i;        //Total number of recorded words
	printf("Please input the words you want to delete (Chinese or English):\n");
	scanf("%s",del);  //Read the words the user needs to delete
	for (i=0;i<count+1;i++)  //Determine if there are any words in the file that the user wants to delete
    {
		if(!strcmp(del,dcsj[i].px))  //English comparison
          {
           ch=1;  //Change the value of ch so that you can judge whether to find the word later
           printf("The word was found and deleted!\n");
		   count1=i;    //Record the position of the word in the structure array
		   break;       //Exit for loop if word found
		  }
		if(!strcmp(del,dcsj[i].zw))  //Chinese comparison
			{
           ch=1;
           printf("The word was found and deleted!\n");
		   count1=i;
		   break;
		  }
	}
	if(ch==0) //If the word is not found, run this step
	{
		printf("Can't find the word!\n");
		return 0;
		//Menu();
	}
	dcsj[count].j=0;   //Assign a value to the structure array to facilitate the termination of the for loop
	strcpy(dcsj[count+1].px,"###");
	strcpy(dcsj[count+1].zw,"###");
	for(i=count1;i<count;i++)  //Start from the position of the word to be deleted, carry out the for loop, and then "move forward" the word in order to overwrite the word to be deleted
	{
		dcsj[i]=dcsj[i+1];
	}
	i=0;  //Reassign i
	printf("The updated word list is:\n");
	while(dcsj[i].j!=0)   //Output the updated word list and write the updated word into the single word copy file
	    {
		  fprintf(fp2,"%d %s %s\n",i+1,dcsj[i].px,dcsj[i].zw);
		  printf("%d %s %s\n",i+1,dcsj[i].px,dcsj[i].zw);
		  i++;
	    };
    if (remove("D:\\Ctext\\danciben.txt") != 0)  //Delete the file where the original words are stored, and judge whether the deletion is successful
    printf("File deletion failed!\n");
	fclose(fp2); //Close file
	rename("D:\\Ctext\\danciben1.txt","D:\\Ctext\\danciben.txt");  //Rename wordbook copy file to wordbook file
}

design sketch

menu bar

Select "delete word" function

Delete the word "happy"

Before and after the word book

Before deletion:

After deletion:

Easy to know "delete word" subfunction realizes the function of deleting words and updating the word book.

Postscript

(1) The implementation idea of "delete word" function is to read all the information in the word book into the structure first, and then realize the word positioning through circulation. After the "delete word" operation on the structure, the latest word book stored in the structure is the latest word book after the required operation. At this time, write a program to write the word book information in the structure into a new file ("dancib En1), delete the old "danciben" file, and then rename "danciben1" file to "danciben" file, which realizes the word deletion function.
(2) The principle of adding words, modifying words and other functions is the same as that of "deleting words". The author will not repeat here, and the partners who need to modify the above codes can do so as needed.
(3) The above interface is not very beautiful. For partners who need it, please refer to the "printInfo()" interface beautification function mentioned earlier to modify the above code.

Posted by will83 on Tue, 09 Jun 2020 22:47:19 -0700