473,395 Members | 1,341 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,395 software developers and data experts.

Cannot create file for write

Hi,

I am trying to create a file for writing:

FILE * List_of_Files;
char * List = "List_of_Files;

if((List_of_Files = fopen(List, "w")) == NULL)
{
fprintf(stderr, "Cannot write file");
}

I don't understand why it does not open the file for writing. Please help.

Thanks,
Marcia
Nov 14 '05 #1
11 6520
Marcia Hon <ho**@rogers.com> scribbled the following:
Hi, I am trying to create a file for writing: FILE * List_of_Files;
char * List = "List_of_Files; if((List_of_Files = fopen(List, "w")) == NULL)
{
fprintf(stderr, "Cannot write file");
} I don't understand why it does not open the file for writing. Please help.


This code doesn't even compile. Please show us your real code if you
want us to have any hope of analysing it.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 14 '05 #2
"Marcia Hon" <ho**@rogers.com> wrote in message
news:zS******************@twister01.bloor.is.net.c able.rogers.com...
Hi,

I am trying to create a file for writing:
Read the documentation of 'fopen()'. You're passing the
wrong arguments. Your code should not even compile.

#include <stdio.h> /* this header makes the declaration of the
standard i/o functions (e.g. 'fopen()')
visible, so the compiler can check that
you've passed the correct number and
types of arguments. */

int main(void)
{
FILE * List_of_Files;
char * List = "List_of_Files;
char filename[] = "the_name_of_my_file"; /* change to whatever your
actual file name is */

if((List_of_Files = fopen(List, "w")) == NULL)
if((List_of_Files = fopen(filename, "w")) == NULL) /* first arg could be a
literal
string if desired */
{
fprintf(stderr, "Cannot write file");
}

I don't understand why it does not open the file for writing. Please help.


I don't understand why you didn't post the actual (complete,
compilable) code that's giving you trouble. :-)

Also, the 'FILE*' type can only designate a *single* file (technically,
called a 'stream') at a time. I think your identifier 'List_of_Files'
is quite misleading.

-Mike
Nov 14 '05 #3
On Mon, 09 Feb 2004 01:20:04 +0000, Yarblek de Logh wrote:
Hi:

I'm only a beginner in C language, but I think I've seen your fail:

You must put the file name in inverted commas. Try it in this way:

FILE *List_of_Files = fopen ("List","w");

Sorry if my English is not so good.


That would be coorrect if she wanted to open a file called List, but list
is actually a char* to the filename. The quotes goes in the line where she
assigns a string to this pointer:

char * List = "List_of_Files";

Beside the missing final quote int the original of that line (which
indicates that what Marcia posted wasn't the actual code. Marcia read at
the bottom please.) the program looks fine. She might want to look at
"errno" to figure out why fopen() fails.
Marcia:
You'll get a better response of you make a tiny program displaying the
problem, then compile and run the program, then cut and paste the source
in your post so that we all have a compileable example of the problem.
Often you'll actually figure out the problem yourself wile preparing your
example

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #4

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...
Beside the missing final quote int the original of that line (which
indicates that what Marcia posted wasn't the actual code. Marcia read at
the bottom please.) the program looks fine. She might want to look at
"errno" to figure out why fopen() fails.


There is no requirement for (nor prohibition of) 'fopen()'s assigning a
value
to 'errno'. Thus your suggestion is not guranteed to give meaningful
results. The only portable determination one can make is:
Did it succeed, or fail?
-Mike
Nov 14 '05 #5
On Mon, 09 Feb 2004 09:58:32 +0000, Mike Wahler wrote:

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid... There is no requirement for (nor prohibition of) 'fopen()'s assigning a
value
to 'errno'. Thus your suggestion is not guranteed to give meaningful
results. The only portable determination one can make is:
Did it succeed, or fail?


Yes, my suggestion was because the code she posted looked OK (aside from
the missing quote") so maybe errno would tell her why it failed on her
plattform.
<ot why="platform specific"> Such as missing write permission to the
directory or something</ot>

errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
printf("Couldn't open file. Error no %d\n");
} else {
printf("Couldn't open file");
}
}

is portable isn't it? Basically:
if(more_information(is_available)) print(it); /* :) */

--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #6
Nils Petter Vaskinn <no@spam.for.me.invalid> wrote:
<snip>
errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
printf("Couldn't open file. Error no %d\n");
} else {
printf("Couldn't open file");
}
}

is portable isn't it? Basically:
if(more_information(is_available)) print(it); /* :) */


Then insert the missing ,errno in the appropriate place
in the code above... ;-)

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...
On Mon, 09 Feb 2004 09:58:32 +0000, Mike Wahler wrote:

"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...

There is no requirement for (nor prohibition of) 'fopen()'s assigning a
value
to 'errno'. Thus your suggestion is not guranteed to give meaningful
results. The only portable determination one can make is:
Did it succeed, or fail?


Yes, my suggestion was because the code she posted looked OK (aside from
the missing quote") so maybe errno would tell her why it failed on her
plattform.
<ot why="platform specific"> Such as missing write permission to the
directory or something</ot>

errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
printf("Couldn't open file. Error no %d\n");
} else {
printf("Couldn't open file");
}
}

is portable isn't it? Basically:
if(more_information(is_available)) print(it); /* :) */


Um, since 'fopen()' isn't required to touch errno, and other
functions are allowed or required to, if one or more of these
other functions were invoked prior to 'fopen()', this takes
the risk of spuriously reporting some other error condition
as being caused by 'fopen()' when it really wasn't.

-Mike
Nov 14 '05 #8
"Mike Wahler" <mk******@mkwahler.net> writes:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...

[...]
errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
printf("Couldn't open file. Error no %d\n");
} else {
printf("Couldn't open file");
}
}

is portable isn't it? Basically:
if(more_information(is_available)) print(it); /* :) */


Um, since 'fopen()' isn't required to touch errno, and other
functions are allowed or required to, if one or more of these
other functions were invoked prior to 'fopen()', this takes
the risk of spuriously reporting some other error condition
as being caused by 'fopen()' when it really wasn't.


Unless you set errno to 0 immediately before calling fopen(), as the
above code does.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #9

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mike Wahler" <mk******@mkwahler.net> writes:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...

[...]
errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
Um, since 'fopen()' isn't required to touch errno, and other
functions are allowed or required to, if one or more of these
other functions were invoked prior to 'fopen()', this takes
the risk of spuriously reporting some other error condition
as being caused by 'fopen()' when it really wasn't.


Unless you set errno to 0 immediately before calling fopen(), as the
above code does.


Um, where's my glasses? :-)

Thanks, Keith.

-Mike
Nov 14 '05 #10
On Mon, 09 Feb 2004 19:36:00 +0100, Irrwahn Grausewitz wrote:
Nils Petter Vaskinn <no@spam.for.me.invalid> wrote:
<snip>
errno = 0;
if (! (file = fopen("blah","w")) {
if (errno) {
printf("Couldn't open file. Error no %d\n");
printf("Couldn't open file. Error no %d\n",errno);
} else {
printf("Couldn't open file");
}
}

is portable isn't it? Basically:
if(more_information(is_available)) print(it); /* :) */


Then insert the missing ,errno in the appropriate place
in the code above... ;-)


Done.

Note te self: Stop typing code directly into newsreader. vi compile run
copy paste post.

:)
--
NPV

"the large print giveth, and the small print taketh away"
Tom Waits - Step right up

Nov 14 '05 #11
Keith Thompson <ks***@mib.org> wrote:
"Mike Wahler" <mk******@mkwahler.net> writes:
"Nils Petter Vaskinn" <no@spam.for.me.invalid> wrote in message
news:pa****************************@spam.for.me.in valid...

[...]
> errno = 0;
> if (! (file = fopen("blah","w")) {
> if (errno) {
> printf("Couldn't open file. Error no %d\n");
> } else {
> printf("Couldn't open file");
> }
> }
>
> is portable isn't it? Basically:
> if(more_information(is_available)) print(it); /* :) */


Um, since 'fopen()' isn't required to touch errno, and other
functions are allowed or required to, if one or more of these
other functions were invoked prior to 'fopen()', this takes
the risk of spuriously reporting some other error condition
as being caused by 'fopen()' when it really wasn't.


Unless you set errno to 0 immediately before calling fopen(), as the
above code does.


Correct; but the IMHO more important aspect is, that the code
avoids evaluating errno in the no-error case: fopen is allowed
to set errno to a non-negative value, though it didn't fail!
(C99 7.5#3 in conjunction with 7.19.5.3 combined with a
sufficiently malicious implemented library.)

So, *first* using the error detection mechanism guaranteed
by the standard (fopen's return value), and *then* checking
the (in case of fopen) implementation-defined value of errno
is The Way To Go - like Nils did.

Just my 2ct.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #12

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

Similar topics

6
by: Christopher Brandsdal | last post by:
Hi! I get an error when I run my code Is there any other way to get te information from my form? Heres the error I get and the code beneath. Line 120 is market with ''''''''''''Line...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
0
by: Zed | last post by:
I have a problem with .net writing to a remote server that I need to deploy on. The app works fine on my local machine. But when got access to the server I went to VS.NET and tried to create a...
22
by: Smutny30 | last post by:
Hello, I am preparing a database that will store 10 n * GBs - 100 n * GBs of data. I calculated to have 1,2 GB of bufferpools. I run the DB2 v. 8.2.1 alone on 4 GB box. I obtain : ...
3
by: Amjad | last post by:
Hi, I just wrote a test Windows Service that creates a text file on startup (please see my code below). The file is never created. Protected Overrides Sub OnStart(ByVal args() As String) Dim...
6
by: sambuela | last post by:
How can I write message to the file located in the wwwroot directory? It seems that IIS protect these files. Let make me cannot do the I/O writing sucessfully. I try to open file's write...
3
by: floppyzedolfin | last post by:
Hi there. I'm coding an encryption / decryption program. At this very moment, I think I should be pretty close from the end, but there's something blocking me on my way. There's a "Padding is...
1
by: pauld | last post by:
Hello - I have created a simple web application in VS 2005/ASP.net 2.0 for a university department to track problems with a new website that I have built for them. The app consists of an Access...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.