Thread: how to make fopen create file if the file doesn't exist?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question how to make fopen create file if the file doesn't exist? (Still not answered)

    The topic says it all.

    I'm writing data to a binary file and I get NULL if the file doesn't exists.

    I know Elysia said in a past post
    You should note that unless the file x.txt exists, then a call to fopen with trying to open a file for reading using "r" will fail.
    Make sure the file exists first, then you can read first and write later.
    Is there no way to force it to create the file if it doesn't exist?
    Last edited by kotoko; 06-16-2008 at 12:01 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Open the file in a write mode. If it doesn't exist, it will be created.

    If you don't need write access (but I can't imagine why you'd want to create a new file only to read "nothing" out of it) then once it's open, use freopen() to switch the mode to a read mode.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Hmm I see.

    But I must ave messed something up because it doesn't create file on write either.

    My writting function is
    Code:
    int guardaLista()
    {
    	FILE *ficheiro = fopen(nome, "wb");
    	int cnt = -1;
    	No* auxiliar = cabeca;
    	
    	if (ficheiro != NULL)
    	{
    		for (;auxiliar!=NULL; auxiliar=auxiliar->proximo)
    			cnt = fwrite( auxiliar->restaurante, sizeof(Restaurante), 1, ficheiro);
    		
    		fclose(ficheiro);
    	}
    	else{
    		printf("O ficheiro n existe.\n");
    	}
    	return cnt; /* número de elementos escritos ou -1 */
    }
    I can give you the structs if you want but I'm guessing it's probably some newbie mistake I can't see.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What does it do? Prints "O ficheiro n existe" ?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    It prints that if the FILE pointer is null which will basically will warn me that something has gone wrong.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I'm guessing you don't have permissions to write the file in the directory?

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by kotoko View Post
    It prints that if the FILE pointer is null which will basically will warn me that something has gone wrong.
    Yeah, I mean when you run your program what does it prints? That the file pointer is NULL (thus "O ficheiro n existe."). It crashes?

    The way to do it is fopen() the file on a read mode. If it gives NULL then fclose() and open it in write mode, forcing it to create the file.

    A wild guess is using "w" instead of "wb"

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If the system this is running on is DOS, Windows or older MacOS, then the "b" in "wb" is definitely important when writing binary data (using fwrite() on structures) - otherwise anything that the C runtime thinks is a newline will be replaced or changed, as newline is represented as CR+LF in DOS/Windows, and CR only in MacOS, whilst it is LF in Linux (and thus needs no conversion).

    I would also rewrite the for-loop this way:
    Code:
    	No* auxiliar;
    	...
    		for (auxiliar = cabeca;auxiliar!=NULL; auxiliar=auxiliar->proximo)
    			cnt = fwrite( auxiliar->restaurante, sizeof(Restaurante), 1, ficheiro);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. using MAKE with makefile to create executable file
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-19-2001, 12:49 PM

Tags for this Thread