File Read/Write

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.
 
J

Jack Klein

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.

There is no way in standard C to delete things from the beginning of a
file, other than create a new file and copy into it all things from
the old file that you don't want to delete.
#####
str_data1<space>str_data2<CR>
^^^^

If you mean the ASCII carriage return character here, 0x0d or '\r' in
C implementations that use ASCII as the execution character set, it is
very platform specific whether or not this character should appear in
ordinary text files. Normally in portable C programs the code writes
the '\n' (newline) character, and the compiler's library takes care of
converting this into whatever the platform's operating system expects
to see for end of line indicators in text files.
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.

If you don't want to see the string "#####", why do you write it to
the file in the first place?
Currently, I have following code, which still need to modify and change.
Please advise.

void writefile(char *mystr1, char *mystr2, char *mystr3)
{
boolean cfx;

There is nothing named 'boolean' in any version of standard C. If you
post code with user defined data types, you should include the
definition of the data type.
FILE *fp_w;

fp_w = Fopen(datapath,"mytest.dat","a+r");

What exactly is Fopen? C has a standard library function named
fopen(), which accepts two arguments. Assuming that this is some
function of your own that eventually passes two strings to fopen(),
the mode string "a+r" is not one defined by the C standard library.
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);

If your implementation treats "a+r" file mode the same as "a+", then
seeking to the beginning to the file will only affect reads. The "a+"
mode specifies that all writes will take place at the end of the file
regardless of any use of fseek().
if (!cfx)
{
fp_w = fopen(codepath,"mytest.dat","a+r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,SEEK_SET);
}

If both attempts to open a file fail, fp_w is still NULL here, and
your further calls to fwrite() produce undefined behavior and almost
certainly a crash of some kind.

Also, you are trying to open files in text mode, the default in C. In
which case using fwrite() is dubious at best. Why are you not using
fputs() and fgets(), since you are reading text strings?
// write "#####"

What's wrong with fputs("#####\n", fp_2); ?
fwrite(&mystr1, sizeof(mystr1), 1, fp_w);

There is a very serious mistake in the line above, which indicates a
misunderstanding on your part about how C file input and output work.
From the text description of your problem, you want to write a string
of text here. But mystr1 is a pointer, and sizeof mystr1 is the size
of a pointer to character on your implementation, usually 2 or 4
bytes. It has nothing at all to do with the size of the character
string that mystr1 points to.

This fwrite(), assuming it isn't mangled because you are writing
binary data to a text file, writes the address of mystr1, which is an
automatic variable allocated on entry to the function. This address
has nothing at all to do with the character string itself, and
certainly not when the file is read in a different execution of the
program or even a different function within the program.
// put a space here

The fputc() function will do that nicely.
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");

If you are only going to read from the file, why not just plain "r"
for the open mode?
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);
}

You have the same issue here with the file pointer being NULL if both
opens fail.
// should put a check if got "#####"

The strcmp() function prototyped in said:
while(!feof(fp_r))

This is always the wrong way to read a file. See this:

http://www.eskimo.com/~scs/C-faq/q12.2.html

....in the FAQ for comp.lang.c. A link to the entire FAQ is in my
signature block.
{ temp_char = fgetc(fp_r);

fgetc() returns an int, do not store it in a char variable. See:

http://www.eskimo.com/~scs/C-faq/q12.1.html in the FAQ,
cprintf("%c", temp_char);

No such function as "cprintf" in the standard C library.
}
fclose(fp_r);

}


Thanks.

You seem to have some misunderstandings about file input and output in
C. I'd suggest a good C book and also reading the FAQ for
comp.lang.c, see link below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top