By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

A File I/O Program

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.