|
Re: Please help to write a program that removes the found string.
[Re: Sergmodern]
#316085
03/21/10 11:30
03/21/10 11:30
|
Joined: Sep 2009
Posts: 1,032 Budapest
Aku_Aku
Serious User
|
Serious User
Joined: Sep 2009
Posts: 1,032
Budapest
|
OK, here is my unchecked code. You have to replace with valid names, strings the oldfile,newfile,oldname,newname texts, and the most important part the "we need this line" condition in the if statement.
STRING* mystring = "";
var eof = 0;
var chars = 0;
fhandle1 = file_open_read(oldfile);
fhandle2 = file_open_write(newfile);
while(!eof) {
chars=file_str_read(fhandle1, mystring);
if(-1 == chars)
eof = 1;
else
if(we need this line)
file_str_write(fhandle2, mystring);
}
file_close(fhandle1);
file_close(fhandle2);
file_delete(oldfile);
file_rename(oldname,newname);
Good luck!
Last edited by Aku_Aku; 03/21/10 11:31.
|
|
|
Re: Please help to write a program that removes the found string.
[Re: Aku_Aku]
#316089
03/21/10 12:02
03/21/10 12:02
|
Joined: Jul 2002
Posts: 4,436 Germany, Luebeck
Xarthor
Expert
|
Expert
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
|
Same idea here. This is Aku_Aku's concept written out and tested plus wrapped into a function:
void file_DeleteRow(STRING* _file, STRING* _searchString)
{
var fhandle_read;
var fhandle_write;
STRING* newFile = "temp";
STRING* row = "#100";
fhandle_read = file_open_read(_file);
if(fhandle_read)
{
fhandle_write = file_open_write(newFile);
while(file_str_read(fhandle_read,row) >= 0)
{
if(str_stri(row,_searchString) == 0) // searchString not found?
{
file_str_write(fhandle_write,row);
file_asc_write(fhandle_write,13);
file_asc_write(fhandle_write,10);
}
}
file_close(fhandle_read);
file_close(fhandle_write);
file_delete(_file);
file_rename(newFile,_file);
}
else
{
error("File not found.");
}
}
Use it like this:
file_DeleteRow("myFile.txt","1222");
This would delete the article "Iron Tefal" of your example.
|
|
|
Re: Please help to write a program that removes the found string.
[Re: Sergmodern]
#316144
03/21/10 19:18
03/21/10 19:18
|
Joined: Mar 2010
Posts: 26
Sergmodern
OP
Newbie
|
OP
Newbie
Joined: Mar 2010
Posts: 26
|
I can't to install the file structure so that all data were put into a file with specific gaps. when viewing information about the products displayed exact same structure as in the file.=(( Or please ask to me how to write the text of program,that delete the find string in the file,if i have next structure of my file:
Article Naim Model Price Date 1234, Iron, Tefal, 100$, 18.03.2010, 2534, Washing Mashine, Samsung, 400$, 12.02.2010
Here deletes only code of product 1234 and delete all "," and file has nex structure:
Iron Tefal 100$ 18.03.2010
Last edited by Sergmodern; 03/22/10 06:17.
|
|
|
Re: Please help to write a program that removes the found string.
[Re: Sergmodern]
#316201
03/22/10 11:02
03/22/10 11:02
|
Joined: Mar 2010
Posts: 26
Sergmodern
OP
Newbie
|
OP
Newbie
Joined: Mar 2010
Posts: 26
|
I have next structure of my program:
typedef struct {
char* art; //Code of product
char* naim; //Name of product
char* model;
int* cena; //Price
char* year;
char* pro; //Country 3 characters
} TOVARS;
TOVARS my_tov[1000];
var number_of_str = 0; // keeps the number of stored strings
var save_enabled = 0; // will be set to 1 when the information has been updated
var index;
TEXT* temp_art = {strings = 1000;} // these arrays store the data that is read using file_str_readto in main
TEXT* temp_naim = {strings = 1000;} // for the authors, books, years and editors
TEXT* temp_model = {strings = 1000;}
TEXT* temp_cena = {strings = 1000;}
TEXT* temp_year = {strings = 1000;}
TEXT* temp_pro = {strings = 1000;}
STRING* art_str = " "; // holds up to 4 characters
STRING* naim_str = " "; // holds up to 20 characters
STRING* model_str = " "; // holds up to 20 characters
STRING* cena_str = " "; // holds up to 20 characters
STRING* year_str = " "; // holds up to 20 characters
STRING* pro_str = " "; // holds up to 20 characters
function save_data()
{
//if (!save_enabled) {return;} // don't allow saving if the data hasn't been updated
var filehandle;
index = -1;
filehandle = file_open_write ("strings1.txt"); // open this file (or create and open it)
file_var_write(filehandle, number_of_str); // write the number of strings
file_close(filehandle); // now close the file
wait (3);
filehandle = file_open_write ("Tovar.txt"); // open the database.txt file
while (index < (number_of_str - 1))
{
index += 1;
my_tov[index].art = (temp_art.pstring)[index]; // copy the information from the strings to the struct
my_tov[index].naim = (temp_naim.pstring)[index];
my_tov[index].model = (temp_model.pstring)[index];
my_tov[index].cena = (temp_cena.pstring)[index];
my_tov[index].year = (temp_year.pstring)[index];
my_tov[index].pro = (temp_pro.pstring)[index];
file_str_write(filehandle, (temp_art.pstring)[index]); // write the information to the file using commas as separators
file_str_write(filehandle, ",");
file_str_write(filehandle, (temp_naim.pstring)[index]);
file_str_write(filehandle, ",");
file_str_write(filehandle, (temp_model.pstring)[index]);
file_str_write(filehandle, ",");
file_str_write(filehandle, (temp_cena.pstring)[index]);
file_str_write(filehandle, ",");
file_str_write(filehandle, (temp_year.pstring)[index]);
file_str_write(filehandle, ",");
file_str_write(filehandle, (temp_pro.pstring)[index]);
file_str_write(filehandle, ",");
}
save_enabled = 0; // disable saving until new changes are made to the database
file_close(filehandle);
}
function add_product()
{
inkey_active=0;
str_cpy(art_str, " ");
set (art_txt, VISIBLE);
inkey(art_str); // read the code of product
//str_cat(art_str," ");
str_cpy((temp_art.pstring)[number_of_str], art_str);
str_cpy(naim_str, " ");
set (naim_txt, VISIBLE);
inkey(naim_str); // read the author
//str_cat(naim_str," ");
str_cpy((temp_naim.pstring)[number_of_str], naim_str);
str_cpy(model_str, " ");
set (model_txt, VISIBLE);
inkey(model_str); // read the author
//str_cat(model_str," ");
str_cpy((temp_model.pstring)[number_of_str], model_str);
str_cpy(cena_str, " ");
set (cena_txt, VISIBLE);
inkey(cena_str); // read the author
//str_cat(cena_str," ");
str_cpy((temp_cena.pstring)[number_of_str], cena_str);
str_cpy(year_str, " ");
set (year_txt, VISIBLE);
inkey(year_str); // read the author
//str_cat(year_str," ");
str_cpy((temp_year.pstring)[number_of_str], year_str);
str_cpy(pro_str, " ");
set (pro_txt, VISIBLE);
inkey(pro_str); // read the author
//str_cat(pro_str," ");
str_cpy((temp_pro.pstring)[number_of_str], pro_str);
my_tov[index].art = art_str; // copy the information from the strings to the struct
my_tov[index].naim = naim_str;
my_tov[index].model = model_str;
my_tov[index].cena = cena_str;
my_tov[index].year = year_str;
my_tov[index].pro = pro_str;
number_of_str += 1; // increment number_of_strings
save_data();
}
void main()
{
video_set(640,480,32,0);
mouse_mode=4;
mouse_map=cursor;
level_load("");
wait(1);
vec_set(sky_color,vector(0,0,0));
index = -1;
var filehandle;
filehandle = file_open_read("strings1.txt"); // try to open the file
if (filehandle) // the file exists?
{
number_of_str = file_var_read(filehandle); // then read number_of_str
file_close(filehandle); // close the file
if (number_of_books > 0) // we've got some products in the database?
{
filehandle = file_open_read("Tovar.txt"); // then open the file
if (filehandle) // the file exists?
{
while (index < (number_of_str - 1)) // go through all the records and store the data that was read in the strings
{
index += 1;
file_str_readto(filehandle, (temp_art.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].art = (temp_art.pstring)[index];
file_str_readto(filehandle, (temp_naim.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].naim = (temp_naim.pstring)[index];
file_str_readto(filehandle, (temp_model.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].model = (temp_model.pstring)[index];
file_str_readto(filehandle, (temp_cena.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].cena = (temp_cena.pstring)[index];
file_str_readto(filehandle, (temp_year.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].year = (temp_year.pstring)[index];
file_str_readto(filehandle, (temp_pro.pstring)[index], ",", 50); // use , as a separator, read up to 50 characters
my_tov[index].pro = (temp_pro.pstring)[index];
}
}
}
}
else // strings1.txt doesn't exist
{
number_of_books = 0;
}
add_product();
}
|
|
|
Re: Please help to write a program that removes the found string.
[Re: Sergmodern]
#316202
03/22/10 11:04
03/22/10 11:04
|
Joined: Mar 2010
Posts: 26
Sergmodern
OP
Newbie
|
OP
Newbie
Joined: Mar 2010
Posts: 26
|
So i have next structure in the file Tovar.txt.
Article Naim Model Price Date 1234, Iron, Tefal, 100$, 18.03.2010, 2534, Washing Mashine, Samsung, 400$, 12.02.2010
Here deletes only code of product 1234 and delete all "," and file has nex structure:
Iron Tefal 100$ 18.03.2010
|
|
|
|