473,763 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,S EEK_SET);
if (!cfx)
{
fp_w = fopen(codepath, "mytest.dat","a +r");
cfx=(fp_w != NULL);
if (cfx) fseek(fp_w,0L,S EEK_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,S EEK_SET);
if (!cfr)
{
fp_r = fopen(datapath, "mytest.dat","a +r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,S EEK_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 4308
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,S EEK_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,S EEK_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
misunderstandin g 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,S EEK_SET);
if (!cfr)
{
fp_r = fopen(datapath, "mytest.dat","a +r");
cfr=(fp_r != NULL);
if (cfr) fseek(fp_r,0L,S EEK_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 misunderstandin gs 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.l earn.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
10141
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 this while experimenting, trying to figure out the file objects modes,
3
2708
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. > > Can somebody please tell whats wrong?? > > This is the code : : #include<fstream.h> #include<conio.h>
0
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
8
23906
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
11153
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 writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
3
8297
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 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
1
64189
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 article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
5605
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 execute it using tomcat server5.5. the code is as follows. if you have any other coding regarding this, please send me.it's urgent. import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;
0
1703
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 pass... ex: pass: 01 11110000 - file 01010101 - pass (repeated until EOF) 10100101 - resulting file decryption would be the exact same process, but i've made it slightly more complicated. first at the beginning of the file I have a...
15
9439
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. http://www.mscibarra.com/webapp/indexperf/excel?priceLevel=0&scope=0&currency=15&style=C&size=36&market=1897&asOf=Jul+25%2C+2008&export=Excel_IEIPerfRegional Want to write python to download and save the file. So far I've arrived at this:
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.