473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_archi ves( .. );

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

for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(. .. );
}
else
{
write_data_firs t(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt LOG_CNT_MAX )
{
write_data_max( ..);
}
else
{
write_data_notm ax( ... );
}
}
void write_date_firs t( ... )
{
create_first_lo g(..);
}

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

Aug 22 '08 #1
1 1450
arnuld <su*****@invali d.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_archi ves( .. );

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

for (i = 0; i != 3; ++i )
{
if( log_cnt )
{
write_datacnt(. .. );
}
else
{
write_data_firs t(..);
}
}
void write_data_cnt( .. )
{
if( log_cnt LOG_CNT_MAX )
{
write_data_max( ..);
}
else
{
write_data_notm ax( ... );
}
}
void write_date_firs t( ... )
{
create_first_lo g(..);
}
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
12369
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 the file myclass.class? Will this work if it's run from a relative path, like: java ../progs/myclass
9
3207
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
3
2817
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 "Thin Client". The problem is that we also use PC's to run the "Client Access" program and under the following script it will create a match and load the wrong WS file. I read that you should not use the DIR function but that's that only way I...
6
5305
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 report any error while writing to the open file handle. On Windows, file shows-up again in explorer and automatically deleted finally when program ends. On Unix, same thing happens if file is on nfs mounted drive, but in this case, actual file is...
4
3657
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 *p_afile; if((p_afile = fopen("shoppinglist.txt", "r")) == NULL) { fprint("Could not open file");
8
9764
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 problem: my vb.net program has problems with UNC. If the UNC server is restarted or goes off-line, my VB.net program crashes. The code for UNC access to the file is included below and is put in the tick event of a form timer control running every...
3
4104
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. 1. I have very much data to be incorporated into the executable file. I have to add much data into my developed program into 2 Combo Boxes and 1 List Box control. For that i have created an xml element with all of the
9
8392
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. However, after I alter the table and open microsoft excel to look at any changes; I get the following error: "This file is not in a recognizable format" If I do open the file in excel it looks like its not formatted.
5
7866
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 setting is of course stored in the app.exe.config file located in the same directory as the exe. Upon closing the form, I save the user setting which then creates a user.config file in the appdata directory in my profile. This is all well and good....
0
1976
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 correctly so that i can allow certain indexes to be removed without completely deleting everything else. This is what i have so far. Code is below It's in the e part of the code and I have //remed the location of the for loop containg the...
0
9685
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
9535
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
9061
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
7558
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
6800
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
5453
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
4127
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
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.