473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

file operations FAQ etc

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.
--
"Windows [n.]
A thirty-two bit extension and GUI shell to a sixteen bit patch to an eight
bit operating system originally coded for a four bit microprocessor and
sold by a two-bit company that can't stand one bit of competition."

Nov 14 '05 #1
4 1295
Douglas G wrote:
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 ... ");
}

--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from my email to mail me.'
Nov 14 '05 #2

"Douglas G" <dg*******@houston.rr.com> wrote

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);
}
....
Nov 14 '05 #3
Malcolm wrote:
"Douglas G" <dg*******@houston.rr.com> wrote
.... snip ...
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;
}


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4

"CBFalconer" <cb********@yahoo.com> wrote
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;
}


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.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter | last post by:
Hello, Two newbie questions: 1) I have a javascript file with a function in it. From this function I want to access a variable in another javascript file -which is not inside a function. I...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
3
by: KittyKit | last post by:
Hello there I've a website that allows a user to create and download a document. I first save the file in the server and then let the user download it. This works just fine. But when multiple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.