Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string. How can I modify the code below, since the code below take...
#5: Post edited
- I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
- How can I modify the code below, since the code below takes line number as input to delete a specific line.
- Thank you.
- ```
- #include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
- How can I modify the code below, since the code below takes line number as input to delete a specific line.
- Thank you.
- ```
- #include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- printf(" \n Enter line number of the line to be deleted:");
- scanf("%d", &delete_line);
- //open new file in write mode
- fileptr2 = fopen("replica.c", "w");
- ch = 'A';
- while (ch != EOF)
- {
- ch = getc(fileptr1);
- //except the line to be deleted
- if (temp != delete_line)
- {
- //copy all lines in file replica.c
- putc(ch, fileptr2);
- }
- if (ch == '\n')
- {
- temp++;
- }
- }
- fclose(fileptr1);
- fclose(fileptr2);
- remove(filename);
- //rename the file replica.c to original name
- rename("replica.c", filename);
- printf("\n The contents of file after being modified are as follows:\n");
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- fclose(fileptr1);
- return 0;
- }
- ```
- **Example of contents in text file:**
- hello world up
- one two three
- **User input:** hello
- **Updated contents in text file:**
- one two three
#4: Post edited
- I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
- How can I modify the code below, since the code below takes line number as input to delete a specific line.
- Thank you.
- ```
- #include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
printf(" \n Enter line number of the line to be deleted:");scanf("%d", &delete_line);//open new file in write modefileptr2 = fopen("replica.c", "w");ch = 'A';while (ch != EOF){ch = getc(fileptr1);//except the line to be deletedif (temp != delete_line){//copy all lines in file replica.cputc(ch, fileptr2);}if (ch == '\n'){temp++;}}fclose(fileptr1);fclose(fileptr2);remove(filename);//rename the file replica.c to original namerename("replica.c", filename);printf("\n The contents of file after being modified are as follows:\n");fileptr1 = fopen(filename, "r");ch = getc(fileptr1);while (ch != EOF){printf("%c", ch);ch = getc(fileptr1);}fclose(fileptr1);return 0;}```**Example of contents in text file:**hello world upone two three**User input:** hello**Updated contents in text file:**one two three
- I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
- How can I modify the code below, since the code below takes line number as input to delete a specific line.
- Thank you.
- ```
- #include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
#3: Post edited
**Hey I have a question, if lets say I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string... How can I modify the code below, since the code below takes line number as input to delete a specific line. Thank you..**```#include <stdio.h>int main(){- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
while (ch != EOF)- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- printf(" \n Enter line number of the line to be deleted:");
- scanf("%d", &delete_line);
- //open new file in write mode
- fileptr2 = fopen("replica.c", "w");
- ch = 'A';
- while (ch != EOF)
- {
- ch = getc(fileptr1);
- //except the line to be deleted
- if (temp != delete_line)
- {
- //copy all lines in file replica.c
- putc(ch, fileptr2);
- }
- if (ch == '\n')
- {
- temp++;
- }
- }
- fclose(fileptr1);
- fclose(fileptr2);
- remove(filename);
- //rename the file replica.c to original name
- rename("replica.c", filename);
- printf("\n The contents of file after being modified are as follows:\n");
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- fclose(fileptr1);
- return 0;
}```Example of contents in text file:- hello world up
- one two three
User input: helloUpdated contents in text file:- one two three
- I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
- How can I modify the code below, since the code below takes line number as input to delete a specific line.
- Thank you.
- ```
- #include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- printf(" \n Enter line number of the line to be deleted:");
- scanf("%d", &delete_line);
- //open new file in write mode
- fileptr2 = fopen("replica.c", "w");
- ch = 'A';
- while (ch != EOF)
- {
- ch = getc(fileptr1);
- //except the line to be deleted
- if (temp != delete_line)
- {
- //copy all lines in file replica.c
- putc(ch, fileptr2);
- }
- if (ch == '\n')
- {
- temp++;
- }
- }
- fclose(fileptr1);
- fclose(fileptr2);
- remove(filename);
- //rename the file replica.c to original name
- rename("replica.c", filename);
- printf("\n The contents of file after being modified are as follows:\n");
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- fclose(fileptr1);
- return 0;
- }
- ```
- **Example of contents in text file:**
- hello world up
- one two three
- **User input:** hello
- **Updated contents in text file:**
- one two three
#2: Post edited
> **Hey I have a question, if lets say I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string... How can I modify the code below, since the code below takes line number as input to delete a specific line. Thank you..**- ```#include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- printf(" \n Enter line number of the line to be deleted:");
- scanf("%d", &delete_line);
- //open new file in write mode
- fileptr2 = fopen("replica.c", "w");
- ch = 'A';
- while (ch != EOF)
- {
- ch = getc(fileptr1);
- //except the line to be deleted
- if (temp != delete_line)
- {
- //copy all lines in file replica.c
- putc(ch, fileptr2);
- }
- if (ch == '\n')
- {
- temp++;
- }
- }
- fclose(fileptr1);
- fclose(fileptr2);
- remove(filename);
- //rename the file replica.c to original name
- rename("replica.c", filename);
- printf("\n The contents of file after being modified are as follows:\n");
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- fclose(fileptr1);
- return 0;
- }```
- Example of contents in text file:
- hello world up
- one two three
- User input: hello
- Updated contents in text file:
- one two three
- **Hey I have a question, if lets say I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string... How can I modify the code below, since the code below takes line number as input to delete a specific line. Thank you..**
- ```#include <stdio.h>
- int main()
- {
- FILE *fileptr1, *fileptr2;
- char filename[40];
- char ch;
- int delete_line, temp = 1;
- printf("Enter file name: ");
- scanf("%s", filename);
- //open file in read mode
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- //rewind
- rewind(fileptr1);
- printf(" \n Enter line number of the line to be deleted:");
- scanf("%d", &delete_line);
- //open new file in write mode
- fileptr2 = fopen("replica.c", "w");
- ch = 'A';
- while (ch != EOF)
- {
- ch = getc(fileptr1);
- //except the line to be deleted
- if (temp != delete_line)
- {
- //copy all lines in file replica.c
- putc(ch, fileptr2);
- }
- if (ch == '\n')
- {
- temp++;
- }
- }
- fclose(fileptr1);
- fclose(fileptr2);
- remove(filename);
- //rename the file replica.c to original name
- rename("replica.c", filename);
- printf("\n The contents of file after being modified are as follows:\n");
- fileptr1 = fopen(filename, "r");
- ch = getc(fileptr1);
- while (ch != EOF)
- {
- printf("%c", ch);
- ch = getc(fileptr1);
- }
- fclose(fileptr1);
- return 0;
- }```
- Example of contents in text file:
- hello world up
- one two three
- User input: hello
- Updated contents in text file:
- one two three
#1: Initial revision
How can I modify the code above to accept string as user input and use strcmp to compare with the contents of the text file & then delete that line?
> **Hey I have a question, if lets say I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string... How can I modify the code below, since the code below takes line number as input to delete a specific line. Thank you..** ```#include <stdio.h> int main() { FILE *fileptr1, *fileptr2; char filename[40]; char ch; int delete_line, temp = 1; printf("Enter file name: "); scanf("%s", filename); //open file in read mode fileptr1 = fopen(filename, "r"); ch = getc(fileptr1); while (ch != EOF) { printf("%c", ch); ch = getc(fileptr1); } //rewind rewind(fileptr1); printf(" \n Enter line number of the line to be deleted:"); scanf("%d", &delete_line); //open new file in write mode fileptr2 = fopen("replica.c", "w"); ch = 'A'; while (ch != EOF) { ch = getc(fileptr1); //except the line to be deleted if (temp != delete_line) { //copy all lines in file replica.c putc(ch, fileptr2); } if (ch == '\n') { temp++; } } fclose(fileptr1); fclose(fileptr2); remove(filename); //rename the file replica.c to original name rename("replica.c", filename); printf("\n The contents of file after being modified are as follows:\n"); fileptr1 = fopen(filename, "r"); ch = getc(fileptr1); while (ch != EOF) { printf("%c", ch); ch = getc(fileptr1); } fclose(fileptr1); return 0; }``` Example of contents in text file: hello world up one two three User input: hello Updated contents in text file: one two three