file operations FAQ etc

D

Douglas G

I'm looking for some links or something that discusses opening files,
writing files, and any gotchas associated with it. I'm new to programming
and C so any pointers would be helpful.

I know this is a generic question, but what I've found from googling shows
stuff that I've already read on that leaves me skeptical as they use
functions like scanf that from what I've seen, you're not to use. If
anyone has helpful links, it would be most appreciated.

I'd just like some ideas on checking whether a file already exists and baily
out gracefully if it does exist.
 
K

Karthik Kumar

Douglas said:
I'm looking for some links or something that discusses opening files,
writing files, and any gotchas associated with it. I'm new to programming
and C so any pointers would be helpful.

Get a good C book. A good book would describe files.
I know this is a generic question, but what I've found from googling shows
stuff that I've already read on that leaves me skeptical as they use
functions like scanf that from what I've seen, you're not to use. If
anyone has helpful links, it would be most appreciated.

Refer to the C FAQ - http://www.eskimo.com/~scs/C-faq/top.html .
I'd just like some ideas on checking whether a file already exists and baily
out gracefully if it does exist.

FILE * fp;

fp = fopen("myfile.txt", "r");
if (fp) {
fclose(fp); /* Release resources */
} else {
perror("I could not open this file ... ");
}
 
M

Malcolm

Douglas G said:
I'm looking for some links or something that discusses opening files,
writing files, and any gotchas associated with it. I'm new to programming
and C so any pointers would be helpful.

I know this is a generic question, but what I've found from googling shows
stuff that I've already read on that leaves me skeptical as they use
functions like scanf that from what I've seen, you're not to use. If
anyone has helpful links, it would be most appreciated.
The problem is errors in input. C has no exception handling mechanism, and
file formats can be very compicated. Sometimes it is OK to terminate the
program with an error message on bad input, but often this isn't an option.

Examples of gotchas.

lines too long to fit in the fgets() buffer.
strings of digits too long to be converted to numbers.
premature termination of file.
delimiters (eq quotes) not properly positioned.
I'd just like some ideas on checking whether a file already exists and baily
out gracefully if it does exist.
int fileexists(const char *path)
{
FILE *fp;

fp = fopen(path, "rb");
if(!fp)
return 0;
fclose(fp);
return 1;
}

....
if( fileexists("Myfile.txt") )
{
printf("It already exists folks\n");
exit(EXIT_FAILURE);
}
....
 
C

CBFalconer

Malcolm said:
int fileexists(const char *path)
{
FILE *fp;

fp = fopen(path, "rb");
if(!fp)
return 0;
fclose(fp);
return 1;
}

The fopen can fail even if the file exists. Better to look for a
better program scheme.
...
if( fileexists("Myfile.txt") )
{
printf("It already exists folks\n");
exit(EXIT_FAILURE);
}
...

and even if filexists works, the answer may be different by the
time the program gets here.
 
M

Malcolm

CBFalconer said:
The fopen can fail even if the file exists. Better to look for a
better program scheme

and even if filexists works, the answer may be different by the
time the program gets here.
This is true. I think the fopen() method is good enough for most purposes,
and there isn't a standard library stat() equivalent.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top