Locate the string "yoons" in a text file whose content has already been created.
I wrote a source with the intention of modifying it to 'h' instead of 'y'.
Read the file in "rt" mode and search for "yoons" to find out where y is in the file.
Exit the "rt" mode stream, open the stream in "at" mode, and instruct the fseek to locate the 'y' in the file Back,
You are trying to modify y by entering h in the location of y through fputc('h',write_data);
H is automatically created at the end of the file as the location is always moved to the end of the file.
In the attached photo file, 15 is the position of y and 43 is the most end position of the file.
Obviously, the location of the file was specified through the location indicator and the word was entered through fputc,
Why do you move back to the end and add it?A useful function or solution that can solve this problem
Please.
Source
void Display() {
system("cls");
printf("***** MENU *****\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Print All\n");
printf("5. Exit\n");
printf("Choose the item : ");
}
void Deco_Display(int ch) {
switch(ch)
{
case 1 : printf("[ INSERT ] \n"); break;
case 2 : printf("[ DELETE ] \n"); break;
case 3 : printf("[ SEARCH ] \n"); break;
case 4 : printf("[ PRINT ALL DATA ] \n"); break;
case 5 : printf("Exit The Program...\n\n");return;
}
}
typedef struct datafile {
char name[15];
char number[15];
}DF;
void Insert() {
FILE * write_data = fopen("data.txt","at");
DF in_value;
char cut_ch = ' ';
char str_ch = '\n';
printf("Input Name : ");
scanf(" %s",in_value.name);
printf("Input Tel Number : ");
scanf(" %s",in_value.number);
fputc(cut_ch,write_data);
fputs(in_value.name,write_data);
fputc(cut_ch,write_data);
fputs(in_value.number,write_data);
printf("\n Data Inserted\n");
fclose(write_data);
}
void Printdata() { FILE * read_data = fopen("data.txt","rt");
char name[15];
char number[15];
while(feof(read_data)==0)
{
fscanf(read_data,"%s %s",name,number);
printf("Name : %s Tel: %s \n\n",name,number);
}
fclose(read_data);
}
void Search() {
FILE * read_data = fopen("data.txt","at");
chars_name[15]; // name to search for
chardata_name[15]; // extraction statement to compare from file
char data_number[15];
fseek(read_data,0,SEEK_SET); // Initialize search location for the first time
printf("Search Name : ");
scanf("%s",s_name); // Enter the name of the target to search for
while(1)
{
fscanf(read_data,"%s %s",data_name,data_number);
If(strcmp(s_name,data_name)==0) // Read data and compare it with input statements
{
printf("\n\nName : %s Tel: %s \n\n",data_name,data_number);
break;
}
if(feof(read_data)!=0) // When the end of the file is reached
{
printf("Scanned data does not exist...\n\n");
break;
}
}
fclose(read_data);
}
void Delete() {
char s_name[15];
char data_name[15];
char t_name[15];
char t_number[15];
char temp[250]= " ";
char jump[2] = " ";
char d_flag = 0;
char i = 0;
int str_num = 0;
long pos = 0;
FILE * read_data = fopen("data.txt","rt");
FILE * write_data = fopen("data.txt","a+t");
fseek(read_data,0,SEEK_SET);
printf("Search Name : ");
scanf("%s",s_name); // Enter the name of the target to search for
For(i=0;i<15;i++) // Calculate the name length of the search target FOR statement
{
if(s_name[i]=='\0')
{
str_num = i;
break;
}
}
while(1)
{
fscanf(read_data,"%s",data_name);
if(strcmp(s_name,data_name)==0)
{
fseek(read_data,-(str_num),SEEK_CUR);
pos = ftell(read_data);
fscanf(read_data,"%s%s",t_name,t_number); // statement to delete
while(feof(read_data)==0)
{
fscanf(read_data,"%s %s",t_name,t_number);
strcat(temp,t_name);
strcat(temp,jump);
strcat(temp,t_number);
strcat(temp,jump);
}
fclose(read_data); // end of read
d_flag = 1;
printf("\npositioning POS : %d\n",pos);
fseek(write_data,pos,SEEK_SET);
fputc('h',write_data);
printf("\nPOS: %d\n", ftell(write_data));
fclose(write_data);
break;
}
if(feof(read_data)!=0) // When the end of the file is reached
{
printf("Scanned data does not exist...\n\n");
break;
}
}
if(d_flag==0) fclose(read_data);
} void main() {
//FILE * read_data = fopen("data.txt","rt");
//FILE * write_data = fopen("data.txt","wt");
int choice = 0;
while(1)
{
Display(); // Configure the main menu and clear the screen
scanf("%d",&choice); // Select Menu
getchar(); // clear input buffer
Deco_Display(choice);
switch(choice)
{
case 1 : Insert(); break;
case 2 : Delete(); break;
case 3 : Search(); break;
case 4 : Printdata(); break;
case 5 : return;
}
system("pause");
}
return;
}
c
Because write files are opened in a+ mode, output operations will all run from end of file. (The same applies to mode a)
Note: URL
append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist.
© 2024 OneMinuteCode. All rights reserved.