Rayman2
December 11th, 2005, 01:45 PM
hi :D
does any one have and code for deleteing specifiv line in file :S
i need to delete 7lines after line that i read from Boxedit
thanx
olivthill
December 11th, 2005, 10:05 PM
Here is a program deleteing the seventh line after the line starting with "Editbox" in a file named "foo.txt":
#include <stdio.h>
#include <stdlib.h>
int del_a_line(void)
{
FILE *fp_in, *fp_out;
char buf[500]; int i;
if ((fp_in = fopen("foo.txt", "rt")) == NULL) {
fprintf(stderr, "Cannot open file foo.txt\n");
return -1;
}
if ((fp_out = fopen("foo_tmp.txt", "wt")) == NULL) {
fprintf(stderr, "Cannot open file foo_tmp.txt\n");
return -1;
}
// Copy everything till the researched line
for (;;) {
if (fgets(buf, sizeof(buf), fp_in) == NULL)
break;
if (fputs(buf, fp_out) < 0) {
fprintf(stderr, "Cannot write\n");
fclose(fp_in);
fclose(fp_out);
return -2;
}
if (strncmp(buf, "Boxedit", strlen("Boxedit")) == 0)
break;
}
// Copy six more lines
for (i = 0; i < 6; i++) {
if (fgets(buf, sizeof(buf), fp_in) == NULL)
break;
if (fputs(buf, fp_out) < 0) {
fprintf(stderr, "Cannot write\n");
fclose(fp_in);
fclose(fp_out);
return -2;
}
}
// Do not copy the seventh line, jusst read it
fgets(buf, sizeof(buf), fp_in);
// Copy until the end of the file
for (;;) {
if (fgets(buf, sizeof(buf), fp_in) == NULL)
break;
if (fputs(buf, fp_out) < 0) {
fprintf(stderr, "Cannot write\n");
fclose(fp_in);
fclose(fp_out);
return -2;
}
}
fclose(fp_out);
fclose(fp_in);
system("DEL foo.txt");
system("REN foo_tmp.txt foo.txt");
return 0;
}
int main(int argc, char *argv[])
{
del_a_line();
system("PAUSE");
return 0;
}
humptydumpty
December 11th, 2005, 10:37 PM
i didn't get your question but if u want to delete a specific line from file you can by
int main ()
{
int i=1;
string line;
ofstream myfile1 ("C:\\File.txt");
ifstream myfile ("C:\\abcd.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
if(i==5)
{
getline (myfile,line);
i++;
continue;
}
getline (myfile,line);
myfile1<<line<<"\n";
i++;
}
myfile.close();
myfile1.close();
}
//remove your abcd.txt file
//and rename new file to old file
return 0;
}
Rayman2
December 12th, 2005, 02:59 AM
oky :D i have made code for deleteing :D i was looking if some method is already writen :D
here is the code if someone will need it :D or if some one can optimaze it
void Shranjevanje::BrisanjeZapisov(int LineAfterWitchIWouldLikeToDelete, int NumOfLinesIwouldLikeToDelete)
{
System::IO::StreamWriter^ sw = gcnew System::IO::StreamWriter(System::Windows::Forms::Application::StartupPath::get()+ "\\" + "tem.tmp");
System::IO::StreamReader^ sw1= gcnew System::IO::StreamReader(System::Windows::Forms::Application::StartupPath::get()+ "\\" + "statistika.dat");
System::String ^line;
int x=0;
while (x<LineAfterWitchIWouldLikeToDelete)
{
line=sw1->ReadLine();
sw->WriteLine(line);
x++;
}
x=0;
while (x<NumOfLinesIwouldLikeToDelete)
{
line=sw1->ReadLine();
x++;
}
while (1)
{
line=sw1->ReadLine();
if (System::String::IsNullOrEmpty(line))
break;
sw->WriteLine(line);
}
sw->Close();
sw1->Close();
System::IO::StreamWriter^ sw3 = gcnew System::IO::StreamWriter(System::Windows::Forms::Application::StartupPath::get()+ "\\" + "statistika.dat");
System::IO::StreamReader^ sw4= gcnew System::IO::StreamReader(System::Windows::Forms::Application::StartupPath::get()+ "\\" + "tem.tmp");
while (1)
{
line=sw4->ReadLine();
if (System::String::IsNullOrEmpty(line))
break;
sw3->WriteLine(line);
}
sw3->Close();
sw4->Close();
}
NoHero
December 12th, 2005, 05:20 AM
[ Moved Thread ]