473,405 Members | 2,334 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,405 software developers and data experts.

A File I/O Program

I am trying to write a program that creates and renames log file on the
system. I have created the initial design ( which of course is not
compilable). Just asking whether this is the right way to do it in C:

/* The Logging Program

* ASSUMPTION: Incoming data (or log) will always be less than LOG_SIZE.

* We will create log files, not more than LOG_CNT_MAX in number. First file will be 0.log.
* Each logfile will have size <= LOG_SIZE. If adding new data makes it larger than LOG_SIZE,
* then we will simply create a new file and rename the old ones. see next paragraph for
* renaming scheme.

* If we will have some logs (say 6 files) already in the system, from 0.log to 5.log, then program
* will preserve them by renaming each file, from 0.log to 1.log, 1.log to 2.log and finally 5.log
* to 6.log. Hence 0.log will always be the latest file

* when LOG_CNT_MAX has reached, then there will be no increment to log count, we will just delete the
* oldest file which is with highest number (LOG_CNT_MAX - 1) and rename each file like we did in
* previous paragraph
*
*
* NOTES:
* File will be opened and closed for 1 time only, it could be written 10 times. It means
* after opening and writing, we will not close the file there. It will remain open. We will
* close it only when writing new data increases its size beyond LOG_SIZE. e.g. If writing
* data 10 times does not reache the alloted limit of LOG_SIZE but 11th write does, then file
* be closed after 11th write.
*
* The above explanation means, we need to have global file pointer always pointing to
* current file being written to. or you have some other better idea.
*
*
* VERSION 1.0 ,
*
* Trying to conform to C90.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define LOG_DIR "/home/arnuld/programs/CLS/"
#define LOG_NAME_FORMAT "%d.log"
enum { LOG_SIZE = 10,
LOG_PATH_SIZE = 50,
LOG_CNT_MAX = 6,
};
int main( void )
{
int log_cnt;
log_cnt = check_for_archives( .. );

start_logging( ... )
return 0;
}
void start_logging( ... )
{
int i;

for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(... );
}
else
{
write_data_first(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt LOG_CNT_MAX )
{
write_data_max(..);
}
else
{
write_data_notmax( ... );
}
}
void write_date_first( ... )
{
create_first_log(..);
}

--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Aug 22 '08 #1
1 1427
arnuld <su*****@invalid.addresswrites:
I am trying to write a program that creates and renames log file on the
system. I have created the initial design ( which of course is not
compilable). Just asking whether this is the right way to do it in C:

/* The Logging Program

* ASSUMPTION: Incoming data (or log) will always be less than LOG_SIZE.

* We will create log files, not more than LOG_CNT_MAX in number. First file will be 0.log.
* Each logfile will have size <= LOG_SIZE. If adding new data makes it larger than LOG_SIZE,
* then we will simply create a new file and rename the old ones. see next paragraph for
* renaming scheme.

* If we will have some logs (say 6 files) already in the system, from 0.log to 5.log, then program
* will preserve them by renaming each file, from 0.log to 1.log, 1.log to 2.log and finally 5.log
* to 6.log. Hence 0.log will always be the latest file

* when LOG_CNT_MAX has reached, then there will be no increment to log count, we will just delete the
* oldest file which is with highest number (LOG_CNT_MAX - 1) and rename each file like we did in
* previous paragraph
*
*
* NOTES:
* File will be opened and closed for 1 time only, it could be written 10 times. It means
* after opening and writing, we will not close the file there. It will remain open. We will
* close it only when writing new data increases its size beyond LOG_SIZE. e.g. If writing
* data 10 times does not reache the alloted limit of LOG_SIZE but 11th write does, then file
* be closed after 11th write.
*
* The above explanation means, we need to have global file pointer always pointing to
* current file being written to. or you have some other better idea.
*
*
* VERSION 1.0 ,
*
* Trying to conform to C90.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define LOG_DIR "/home/arnuld/programs/CLS/"
#define LOG_NAME_FORMAT "%d.log"
enum { LOG_SIZE = 10,
LOG_PATH_SIZE = 50,
LOG_CNT_MAX = 6,
};
int main( void )
{
int log_cnt;
log_cnt = check_for_archives( .. );

start_logging( ... )
return 0;
}
void start_logging( ... )
{
int i;

for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(... );
}
else
{
write_data_first(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt LOG_CNT_MAX )
{
write_data_max(..);
}
else
{
write_data_notmax( ... );
}
}
void write_date_first( ... )
{
create_first_log(..);
}
The problem I have with it its that this outline code has nothing to
do with what the comment says about what should happen. Where does
start_logging come from with its loop? Why is there not function to
"cycle the log files"?

The second issue (which might be related, I don't know) is that you
describe the logging but not what the program should do! It this a
test program to test a library of logging functions or is it itself a
data logging program? If it is the former, you need to specify the
logging API before doing anything else (it may change, but specify it
now so you know what to implement). If it is the latter, tell us what
the whole program should do: where does it get the data from, how much
in any one execution, etc? Currently is has not command-line arguments
(very odd) and seems to do no input (even odder).

--
Ben.
Aug 22 '08 #2

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

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
3
by: Joe Costa | last post by:
I have written the following code to search for the right file depending on the startup file for "Client Access". The menu database that I made will load the correct config file specific for each...
6
by: Kiran | last post by:
Hi, I have program, which opens file at the startup and logs error messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not...
4
by: Frank | last post by:
Could someone tell me how to open a file at run time that I didn't know the name of at compile time? I know how to open a file at compile time when I know what the name is going to be. FILE...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
3
by: SpIcH | last post by:
Hi All, This is all about protecting my data in Executable file. I have developed a program in Visual Basic .NET 2002. I have many questions in mind... please help me to complete my project. ...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
5
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
0
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.