472,954 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

File Read/Write

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.
Nov 14 '05 #1
1 4248
On Mon, 5 Jul 2004 10:27:58 +0800, "Magix" <ma***@asia.com> wrote in
comp.lang.c:
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 <string.h> can do this nicely.
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
1
by: shyaminf | last post by:
hi everybody! iam facing a problem with the transfer of file using servlet programming. i have a code for uploading a file. but i'm unable to execute it using tomcat5.5 server. kindly help me how to...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.