473,378 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 4278
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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.