A File I/O Program

A

arnuld

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(..);
}
 
B

Ben Bacarisse

arnuld said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top