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));

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

>>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.

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.

commented: Oops! :) +28

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

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 *

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

>> 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.

could what be a unicode string
how do i check for EINVAL

it works when i use a literal such as f = fopen("vars.rc", "w");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.