M
Magix
Hi,
I have these string data: str_data1, str_data2, str_data3, which capture
some value after a routine process A. Then I would like to write (append)
these 3 string values into a text file each time after routine process A,
the text file is named "mytext.dat" in following format with "#####" as
separator.
The maximum entries of them is 5. When reaching the fifth entry, it will
delete the very first entry.
#####
str_data1<space>str_data2<CR>
str_data3
#####
str_data1<space>str_data2<CR>
str_data3
#####
str_data1<space>str_data2<CR>
str_data3
When I want to read, I would read all of them without the appearance of
#####. The ##### will be replaced by <LF> line feed.
Currently, I have following code, which still need to modify and change.
Please advise.
void writefile(char *mystr1, char *mystr2, char *mystr3)
{
boolean cfx;
FILE *fp_w;
fp_w = Fopen(datapath,"mytest.dat","a+r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);
if (!cfx)
{
fp_w = fopen(codepath,"mytest.dat","a+r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);
}
// write "#####"
fwrite(&mystr1, sizeof(mystr1), 1, fp_w);
// put a space here
fwrite(&mystr2, sizeof(mystr2), 1, fp_w);
// put a line feed here
fwrite(&mystr3, sizeof(mystr3), 1, fp_w);
fclose(fp_w);
}
void readfile(void)
{
boolean cfr;
FILE *fp_r;
char temp_char;
mytest_r = Fopen(datapath,"mytest.dat","a+r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,SEEK_SET);
if (!cfr)
{
fp_r = fopen(datapath,"mytest.dat","a+r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,SEEK_SET);
}
// should put a check if got "#####"
while(!feof(fp_r))
{ temp_char = fgetc(fp_r);
cprintf("%c", temp_char);
}
fclose(fp_r);
}
Thanks.
I have these string data: str_data1, str_data2, str_data3, which capture
some value after a routine process A. Then I would like to write (append)
these 3 string values into a text file each time after routine process A,
the text file is named "mytext.dat" in following format with "#####" as
separator.
The maximum entries of them is 5. When reaching the fifth entry, it will
delete the very first entry.
#####
str_data1<space>str_data2<CR>
str_data3
#####
str_data1<space>str_data2<CR>
str_data3
#####
str_data1<space>str_data2<CR>
str_data3
When I want to read, I would read all of them without the appearance of
#####. The ##### will be replaced by <LF> line feed.
Currently, I have following code, which still need to modify and change.
Please advise.
void writefile(char *mystr1, char *mystr2, char *mystr3)
{
boolean cfx;
FILE *fp_w;
fp_w = Fopen(datapath,"mytest.dat","a+r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);
if (!cfx)
{
fp_w = fopen(codepath,"mytest.dat","a+r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);
}
// write "#####"
fwrite(&mystr1, sizeof(mystr1), 1, fp_w);
// put a space here
fwrite(&mystr2, sizeof(mystr2), 1, fp_w);
// put a line feed here
fwrite(&mystr3, sizeof(mystr3), 1, fp_w);
fclose(fp_w);
}
void readfile(void)
{
boolean cfr;
FILE *fp_r;
char temp_char;
mytest_r = Fopen(datapath,"mytest.dat","a+r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,SEEK_SET);
if (!cfr)
{
fp_r = fopen(datapath,"mytest.dat","a+r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,SEEK_SET);
}
// should put a check if got "#####"
while(!feof(fp_r))
{ temp_char = fgetc(fp_r);
cprintf("%c", temp_char);
}
fclose(fp_r);
}
Thanks.