Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to read a text file line by line, perform some checks, and if the line is not required, delee it. I have done the code for reading line, but I don't know how to delete that line if it is not required by me. Please help me find the simplest method for deleting the line. Here is my code snippet for reading the line:
C
char string[16];
int number;
DWORD dwWritten;
FILE *fp;
HANDLE hFile;
hFile=CreateFile("File.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
WriteFile(hFile,"Line 1\n\nLine 2\n\n",strlen("Line 1\n\nLine 2\n\n"),&dwWritten,0); //write two lines to the file
fp = fopen("File.txt", "r");
while (fscanf(fp, " %s %d", string, &number) > 0) {
        if(number == 2)
        {
         //here I want to delete this line in which number == 2
        }
}
Posted
Updated 1-Apr-13 21:52pm
v2

1 solution

You can't "delete a line" from a text file - to do this, you will have to open a new output file, and copy over the lines you don't want to delete. At the end, close all the files, rename the original to a temporary name, then rename the output file as the original, and delete the original.
 
Share this answer
 
Comments
ayesha hassan 2-Apr-13 3:48am    
Thank you so much. looks useful to me but sounds complex :( I am new in this, can you give me an example?
CPallini 2-Apr-13 3:56am    
Is not that difficult: open the original file for reading, another one for writing. Read the original file line by line. If the line has to be deleted then don't write it to the output file, otherwise write it. close both files. Delete the original one and rename the output one.
OriginalGriff 2-Apr-13 4:08am    
:thumbsup:

I prefer to rename the original (to .BAK) but then I hate losing information! :laugh:
ayesha hassan 2-Apr-13 4:34am    
Thank you so much CPallini.
I followed your instructions and worte this code. Now the line I wanted to delete is successfully removed in my output file, but the last step of deleting the original file and renaming the output file is not working. Please tell me what wrong am I doing.
char ip[32];
int port;
DWORD dwWritten;
FILE *fp;
HANDLE hFile,tempFile;
hFile=CreateFile("Hell.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
tempFile=CreateFile("temp.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
WriteFile(hFile,"10.0.1.25 524192\r\n\r\n10.0.1.25 524193\r\n\r\n",strlen("10.0.1.25 524192\r\n\r\n10.0.1.25 524193\r\n\r\n"),&dwWritten,0);
fp = fopen("Hell.txt", "r+");

while (fscanf(fp, " %s %d", ip, &port) > 0)
{
printf("\nLine1:");
printf("ip: %s, port: %d", ip, port);
char portbuff[32], space[]=" ";
sprintf(portbuff, "%i",port);
strcat(ip," ");
strcat(ip,portbuff);
if(port == 524192)
printf("\n Delete this Line now");
else
WriteFile(tempFile,ip,strlen(ip),&dwWritten,0);
}

remove ("Hell.txt");
rename("temp.txt","Hell.txt");
OriginalGriff 2-Apr-13 4:50am    
You need to close the files before you can delete or rename them - otherwise they are in use by your program and can't be touched!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900