Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i want to remove a line from .txt file based on some key value(LEDbrightness).
for eg: i will have LEDbrightness=2 in .txt file . Either i need to change the value of LEDbrightness from 2 to 3 or i need to remove the entire line LEDbrightness=2 and need to add LEDbrightness=3. I am able to add that. but it is adding to the existing one.
LEDbrightness=2
LEDbrightness=3

i am not able to remove LEDbrightness=2. Please help me on this. i m really stuck in this.
Posted
Comments
ZurdoDev 17-Dec-14 13:18pm    
Where are you stuck? Do you know how to access a text file for read/write in C++?
sherin.prajin 18-Dec-14 0:20am    
i used the below code to append the text into this. i dont know how to replace/remove the existing text.
void WriteintoINI(const char* fpath,string confKey,string confVal){
FILE * pFile;
long size;
confKey.append(confVal);
pFile = fopen ( fpath , "a" );
if (pFile != NULL){

fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fputs (confKey.c_str() , pFile );
fprintf(pFile, "\n");
fclose (pFile);

}
}

1 solution

A simple approach is:
Read the input file line by line and copy to the output only the required/modified ones.
At the end remove the input file and properly rename the output one (for small files you may read the whole file in memory, preform the modifications and then write back it to the disk).
 
Share this answer
 
Comments
Maciej Los 17-Dec-14 15:29pm    
5ed!

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