LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2006, 12:04 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
C program advice write files


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. ?
 
Old 10-11-2006, 12:13 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
it really depends on what these functions do, and how you have it designed..

what does your program do?
 
Old 10-11-2006, 04:11 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.

Last edited by exvor; 10-11-2006 at 04:13 PM.
 
Old 10-12-2006, 11:02 AM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
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..

make sense?
 
Old 10-12-2006, 11:09 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
Old 10-12-2006, 11:30 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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;
}

make sense?
 
Old 10-12-2006, 12:13 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.


Thanks a million xhi
 
Old 10-13-2006, 02:51 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.

Last edited by exvor; 10-13-2006 at 03:13 PM.
 
Old 10-13-2006, 08:51 PM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
yeh extern is the default specifier, so you dont have to add it, i do just for clarity.. im glad that example made sense..

good luck.
 
Old 10-16-2006, 07:47 AM   #10
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
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.

Regards, Schwanbein
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Advice for someone who wants to Program qmjhlpei Programming 6 11-24-2005 03:02 PM
Able to write files to MP3 player but no files written into actual device? olnex Linux - Hardware 0 11-11-2005 06:32 AM
Need to write a Bash Shell any advice? CyberEd55 Programming 3 05-12-2005 01:52 PM
advice me some program to see movie cd vanhelsing Linux - Software 1 05-27-2004 10:57 PM
Write to port (C program) dummyagain Programming 8 10-24-2003 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration