[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

help with file opening /closing

Posted on 2005-04-08
8
Medium Priority
?
274 Views
Last Modified: 2010-04-15
I have two functions A and B ,

A(){
     ....
     B();
     .....
}


B(){
   write some date to logfile.txt;

}

-------------------------
function B is called from function A, now in B i need to write some data to a file , say logfile.txt
now for some reason , I am not allowed to open the logfile  in either the main function or function A.  So I have these following options

1. open/close  the file in B()   in which case I will end up opening/closing the file each time B() is called

2. create the File* fp as a static variable  , so i can open it once and then each time check if its null like this
B(){
  static FILE *fp = NULL;
  if( !fp){  
    fp = fopen(....) ;
 }
 do stuff with fp ;
 .....

}


In this case , I would not know when to close the file .

------------------------

Do you guys have any other way ?
0
Comment
Question by:avizit
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13734031
Hi avizit,

Way 2 is the way to go.
Other options would be to use global variables which is not a good idea ...
It would be interesting to know of contraints which would not allow you to open the file in A()

Cheers!
sunnycoder
0
 
LVL 11

Author Comment

by:avizit
ID: 13734094
>>It would be interesting to know of contraints which would not allow you to open the file in A()

the reason is ,
The call to B() in A() , is not a normal function call as in I don't insert call it explicitly. But I am modifying the interemediate code of the compiled program and inserting a call instruction [ which is a call to B() ]

the process is something like this

A() is in file fileA.c , and B() is in fileB.c
intially in A() there is no call to B() ,

while compiling fileA.c , the compiler inserts a call to B().
and later I link both the compiled object files.  B() is basically a logging function , which I may decide to change to do nothing , so i want everything to do with file pointers to be done in B() itself, otherwise there will be filepointer related junk on A() which may be totally unnecessary.


Hope I confused you enough :)

-------
am leaving this question open for a few days , to see if ppl have other techniques .

Thanks,
abhijit
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 300 total points
ID: 13734403
I got your point ... unless you could modify main, there would not be much sense in opening and closing in A either. ... your way 2 looks appealing to me
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Accepted Solution

by:
PaulCaswell earned 300 total points
ID: 13734577
You have the right method but, to close the file, use 'atexit'.

You have several options:

1. Make the file handle a global to the 'B' module or use some system-specific method of discovering the file handle from the file name.

2. Add a perameter to 'B' that indicates that it should close the file.

Paul
0
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 600 total points
ID: 13737256
Use a kind of instance pattern.

static FILE *logfileptr = NULL;
FILE *getLogFilePtr() {
    if (logfileptr == NULL) {
        logfileptr = fopen("logfilename", "wa");
    }
    return logfileptr;
}

Then, in B, just call getLogFilePtr() to get the pointer.  This way can be used in multiple B-like functions.
0
 
LVL 11

Author Comment

by:avizit
ID: 13747410
oops i meant to accept nova denizen .. .. sorry ..
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13750330
avizit,

Would you like me to reopen the question for you?
0
 
LVL 11

Author Comment

by:avizit
ID: 13750412
if no one objects i guess it doesn't really matter . i have awarded points as per what I wanted .
if anyone objects i am willing to undo my mistake .. but I hope its fine.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question