Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
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 1-Apr-13 22:42pm
Edited 1-Apr-13 22:52pm
CPallini326.1K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
ayesha hassan at 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 at 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 at 2-Apr-13 4:08am
   
:thumbsup:
 
I prefer to rename the original (to .BAK) but then I hate losing information! :laugh:
ayesha hassan at 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 at 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!
ayesha hassan at 2-Apr-13 4:54am
   
Thank you :) I closed the file, then checked. even the rename() function succeeds still the file is not renamed or deleted :(
OriginalGriff at 2-Apr-13 5:07am
   
Did you close everything? You have two handles and a file pointer...
ayesha hassan at 2-Apr-13 4:59am
   
Could it be an issue due to the attributes I used while using CreateFile()?
ayesha hassan at 2-Apr-13 6:38am
   
I see two strange behaviours here.
1. Placing remove() before rename() gives an error while rename()ing the file.
2. Placing remove() after rename() does not rename the file but generates a new file :\
ayesha hassan at 2-Apr-13 6:48am
   
And now another issue, sometimes, rename() fails as well, once in twenty tries.
ayesha hassan at 2-Apr-13 6:54am
   
Below is the latest code that I am trying:
char ip[32];
int port;
DWORD dwWritten;
FILE *fpOriginal, *fpOutput;
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);
fpOriginal = fopen("Hell.txt", "r+");
fpOutput = fopen("temp.txt", "w+");
 
while (fscanf(fpOriginal, " %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);
}
 
fclose(fpOriginal);
fclose(fpOutput);
CloseHandle(hFile);
CloseHandle(tempFile);
remove("Hell.txt");
if(!(rename("temp.txt","Bye.txt")))
{
printf("\ncould not rename\n");
}
else
printf("\nRename Done\n");
//remove ("Hell.txt");

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 273
2 Aajmot Sk 234
3 Marcin Kozub 205
4 Richard MacCutchan 200
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100