0

when i run this code, f is being set to null and i have no idea why..
i know for a fact that cmd.argv(1) at that point is "vars.rc" because i debugged it
with breakpoints.

FILE *f;

	if(cmd.argc() != 2)
	{
		con.printf("writevars: <filename>\n");
		return;
	}

	f = fopen(cmd.argv(1), "w");

	if(!f)
	{
		con.printf("could not open file %s\n", cmd.argv(1));
		return;
	}

	cvar.writevariables(f);

	fclose(f);

	con.printf("vars written to %s\n", cmd.argv(1));
5
Contributors
9
Replies
11
Views
6 Years
Discussion Span
Last Post by tomtetlaw
Featured Replies
  • perror after line 13 can give some useful information. AD: OP tries to open the file for writing; it doesn't matter if the file already exists or not. Read More

0

Works for me (I used a hard-coded file name instead of argument). Do you have the correct permissions on the file and directory?

Edited by Agni: n/a

0

>>i know for a fact that cmd.argv(1) at that point is "vars.rc"

But is that file in the program's current working directory? Try running your program with the full path to the file and see if that works, or move the file into the program's current working directory.

>>if(cmd.argc() != 2)
I may be wrong but that looks more like c++ than C.

Edited by Ancient Dragon: n/a

1

perror after line 13 can give some useful information.

AD: OP tries to open the file for writing; it doesn't matter if the file already exists or not.

Comments
Oops! :)
0

Meaning of "w" is:

Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.

If the file exists and you don't have correct permissions, fopen doesn't erase it and returns null pointer. If you don't have enough rights to create new file in current directory, fopen returns a null pointer (as Agni already stated).

-- tesu

0

calling perror after i check if the file is null prints out "Invalid Argument"
i know that "w" is a valid mode..
i might try casting cmd.argv(1) to const char *

0

when i store cmd.argv(1) in a char *, it works, but it says it's writing to a file called "u(diomond shape)4" then it gets saved to a file called 4
this really makes no sense

0

>> calling perror after i check if the file is null prints out "Invalid Argument"
i know that "w" is a valid mode.

Well, could it be a unicode string?
EINVAL after fopen does mean a bad mode after all.

>>i might try casting cmd.argv(1) to const char *

I doubt it would help. Non-constness may cause compile-time errors; you have a runtime one.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.