473,405 Members | 2,141 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.

null FILE* pointers

With std::fopen, will it return a null pointer if the file does not
exist? If so, how do I make it so that it will attempt to create a
file if it doesn't exist? Would moving to fstreams/filebufs address
these issues?

--
I am only a mirage.
Jul 23 '05 #1
4 1634
The documentation (eg,
http://www.die.net/doc/linux/man/man3/fopen.3.html ) says that if you
open the file for write or append (in addition to read if you'd like)
the file will be created if it doesn't exist.

And std::fstream would be preferred in c++ anyway. stream
insertion/extraction operators (<< and >>) are safer and more
extendable and such than printf/scanf style functions.

Jul 23 '05 #2


kelvSYC wrote:
With std::fopen, will it return a null pointer if the file does not
exist?
It depends on the mode argument to fopen().
If so, how do I make it so that it will attempt to create a
file if it doesn't exist?


That depends on the mode argument to fopen(). Did you read up on that?
Your documentation should explain.


Brian

Jul 23 '05 #3


Default User wrote:
kelvSYC wrote:
With std::fopen, will it return a null pointer if the file does not
exist?


It depends on the mode argument to fopen().


It may also depend on platform-specific things, such as file
permissions. That is, a file may exist but the application not have
permission to open it. That's why the trick of trying to open a file
for reading is not always an adequate test for file existence.

Brian

Jul 23 '05 #4
kelvSYC <ke*****@no.email.shaw.ca> wrote:

(Assuming std::fopen behaves more or less like fopen() from C:)
With std::fopen, will it return a null pointer if the file does not
exist?
fopen returns a null pointer if the attempt to open the file fails.
This can happen for various reasons; one of which, of course, is that
the file does not exist. However, if the mode is "w" or "a", the file
will be created if it does not exist; if you get a null pointer in one
of these modes, something else has presumably gone wrong.
If so, how do I make it so that it will attempt to create a
file if it doesn't exist?
If you're opening for read, you can use "a+" as the mode; this will
open the file for read and write and create the file if it does not
exist.
Would moving to fstreams/filebufs address
these issues?


Probably, but I'll leave the answer to knowledgeable types.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 23 '05 #5

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

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
16
by: mike79 | last post by:
Hi all, I have a the following simple piece of code which has taken me hours to try and sort out the problem, but still unable to find what is wrong. void main( void ) { char (*string);...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
22
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
42
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.