ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I know ive asked a similar question in the past and im looking for a little experianced programmer advice on this one. Im in the process of rewriteing my own program that i quickly put together to make sure I was able to get everything to work and im a little concerned that im doing a fopen on each function that needs to read and write data and closeing them when the function is done. What im wondering is would this be a better aproch then opening the file when the program starts and then working with the file descriptor with my subsequent reads and writes and then closeing the file when the program terminates.
Is there any advantage to ither aproch. ?
It keeps a sort of database of disc information for each cd or DVD in a collection. each disc will have information such as name its number in the collection what condition it was in and a seperate list of acutal files for data cd's / DVD's. the user enters data and it gets stored to a structure element that gets written to the file. it has a way of internally locateing each file and also checking to see if a particlar block should be overwritten. It does this using a random access file and subseqent fread and fwrites. What im wondering is at the moment im passing the name of the data file to each function and they are opening and closing the file internally before passing back control to the main program. but i was considering opening the file when the program is excuted and then passing the file descriptor to the functions so they can modify data.
What im wondering is at the moment im passing the name of the data file to each function and they are opening and closing the file internally before passing back control to the main program. but i was considering opening the file when the program is excuted and then passing the file descriptor to the functions so they can modify data.
well assuming that you are using one file, and will only be using one file, and that file can stay open without a problem..
there is nothing wrong with doing it like that, passing to every function, that is probably a better idea than opening and closing the file every time. what i would do is to make the file handle a static variable within your file access library. when you start the app, you call a library function that opens that file, and when you are exiting you call a library function that closes the file. to read and write to the file you use the api you provide with the library.
this way you can avoid passing the file handle all around through your functions, and you also can control how the file is used and by whom..
Kind of. From what I gather from your message you mean to create a master function that can read write and modify data in the file and then just call that function from main to open it and close it at the end of main. and use options that will be passed to it to control it. I guess that would work as that was what i was considering but not to that extent but it would be eazy to modify things to work that way. Im a little new at this so if im getting what you mean wrong let me know.
yeh youre getting the idea of it. like i said before this depends on your design, do you have multiple header files and do you compile multiple objects before linking them all together? if not this may require alot of changes and you may not want to do it. but here is an example..
myfileio.h
Code:
extern int fileio_initdb();
extern int fileio_closedb();
extern int fileio_write_record(Record* rec, RecType type);
extern int fileio_read_record(Record* rec, RecType type, int record_id);
myfileio.c
Code:
static FILE* records_database=0;
int fileio_initdb()
{
// open db file assigning to records_database
// return status (success / failure)
}
int fileio_closedb()
{
// close records_database
// return status
}
int fileio_write_record(Record* rec, RecType type)
{
// put your record writing logic here and you can access the
// records_database directly without passing it as a param
}
int fileio_read_record(Record* rec, RecType type, int record_id)
{
// put your record reading logic here and you can access the
// records_database directly without passing it as a param
}
main.c
Code:
#include <myfileio.h>
int main()
{
if(!fileio_initdb())
{
// exit noisly because you could not open the db file
}
// now you can call your fileio_read and fileio_write functions directly on your
// records without worrying about keeping track of the file handle. this also proivdes
// an api that you can control (like encapsulation) and makes debugging and changes
// a whole lot easier..
fileio_closedb();
return 0;
}
Yea thats the path I was on when trying to get my head around the problm makeing them extern solves the whole keeping track of the file handle issue that i was trying to purge. Right now the code is messy primarlly because of that.
Why would you need an extern on the functions if they are being added to main as a header file? Wouldent they automaticly be considered external?
as for working with the file handle I figured out what exactly you were refering to. Im in the process of rewriteing it and im implementing some of what you suggested im just confused on that extern to the function. I know what it does but im not sure why it would be needed for main ?
Ok never mind I kinda figured this out on my own the extern is really not required but is just good programming practice as the functions are being delcared not in main but rather a external header file.
You might like to flush() from time to time if you're writing. Just in case the data is valuable and something happens.
The other reason that you might like to open & close the function is that other programs might want to access and modify the same file. Remember that:
1:- If you open a file, close it and open it again, you can't extect to get the same data twice as someone else may have monkeyed with the data. (Reason for opening the file only once). If you must repeatedly open & close a file, don't assume anything about the file contents. Check them.
2:- If multiple programs open a file and modify it, they will overwrite the original when they close the file. In particular the last program to close will be the only version that will survive. If this is a problem, put a lock on the file, making a private copy first if necessary. I haven't ever played with locks though, so I can't help you there. If you have to lock and contention is a problem, you will have to open & close the file repeatedly so that you don't hog it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.