Cannot create file for write

M

Marcia Hon

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
 
J

Joona I Palaste

Marcia Hon said:
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.
 
M

Mike Wahler

Marcia Hon said:
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
 
D

Derk Gwen

# 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)
# {

Do something like

fprintf(stderr,"open file failed: ");
perror(List);

# }

And you're likely to get a much more informative error message.
 
E

E. Robert Tisdale

Marcia said:
I am trying to create a file for writing:

FILE * List_of_Files;
char * List = "List_of_Files;

You forgot the closing double quote:

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.
> cat main.c
#include <stdio.h>

int main(int argc, char* argv[]) {
char* List = "List_of_Files";
FILE* List_of_Files = fopen(List, "w");

if(NULL == List_of_Files) {
fprintf(stderr, "Cannot write file %s\n", List);
}
else {
fprintf(List_of_Files, "# This file: %s\n", List);
}

return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
> cat List_of_Files
# This file: List_of_Files
 
Y

Yarblek de Logh

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.

Greetings:

Yarblek de Logh
 
J

Jack Klein

# 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)
# {

Do something like

fprintf(stderr,"open file failed: ");
perror(List);

# }

And you're likely to get a much more informative error message.

Or just as likely not to, it is all quite implementation defined. The
fopen() function is not required to change the value of errno on
failure. Neither is it forbidden to.
 
N

Nils Petter Vaskinn

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
 
M

Mike Wahler

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
 
N

Nils Petter Vaskinn

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); /* :) */
 
I

Irrwahn Grausewitz

Nils Petter Vaskinn said:
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
 
M

Mike Wahler

Nils Petter Vaskinn said:
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
 
K

Keith Thompson

Mike Wahler said:
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.
 
N

Nils Petter Vaskinn

printf("Couldn't open file. Error no %d\n",errno);
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.

:)
 
I

Irrwahn Grausewitz

Keith Thompson said:
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
 

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
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top