help error writing to file

This is a discussion on help error writing to file within the C Programming forums, part of the General Programming Boards category; Hi! I'm trying to code a program that needs to store data in a binary file. I already did the ...

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

    Question help error writing to file

    Hi!

    I'm trying to code a program that needs to store data in a binary file.
    I already did the first try of the code and it works fine except when the file hasn't been created yet and I try to read. I thought that the fopen fuction would just return NULL in this case.

    My code to read is:

    Code:
    int leLista(char* nome)
    {
    	FILE *ficheiro = fopen(nome, "rb");
    	int cnt = -1;
    	Restaurante* auxiliar;
    
    	if (ficheiro != NULL)
    	{
    		do{
    			auxiliar = (Restaurante*)malloc(sizeof(Restaurante));
    			cnt = fread(auxiliar, sizeof(Restaurante), 1, ficheiro);
    			printf("batatas %s\n", auxiliar->nome);
    			if (cnt == 1)
    			{
    				inserir(criaNo(auxiliar));
    			}
    		}while(cnt != 0);
    
    		imprimeLista();
    	}
    
    
    	int valor = fclose(ficheiro);
    	
    	return cnt;
    }
    It generates a segmentation fault. If I run it in gdb I get
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0xb7eb25dd in fclose () from /lib/tls/i686/cmov/libc.so.6
    (gdb) print errno
    Cannot find thread-local variables on this target
    What am doing wrong?

    Thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I am guessing Restaurante is a struct of sorts that you've written to a binary file. Well, this isn't going to work. You can't simply read auxiliar->nome the same way you wrote it. What is written into the file is the address stored in nome, not the contents of nome. When you read it back, you'll be reading an address that you may or may not have access to. This means nome points to some unknown location when you run the program again.

    The solution is to write your own functions to write and read structs of Restaurante yourself, preferably in text form, to keep it all portable and easy on yourself.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    28
    I would put the fclose inside your ( != NULL) conditional also, otherwise your application may end up tryng to close a NULL pointer (which several OS's don't like you doing).

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

    Nice advice but

    Thanks trillianjedi I'll try to do that.

    In the meanwhile, MacGyver you said

    I am guessing Restaurante is a struct of sorts that you've written to a binary file. Well, this isn't going to work. You can't simply read auxiliar->nome the same way you wrote it. What is written into the file is the address stored in nome, not the contents of nome. When you read it back, you'll be reading an address that you may or may not have access to. This means nome points to some unknown location when you run the program again.
    I don't understand, when I try to open the binary file in text editor, most of it isn't understandable but the name is there I can read it. Maybe I understood you wrong?
    Also I need to use binary files, it's part of the assignment

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Thanks so much trillianjedi it works fine (so far ^_^ )

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, if it's part of the assignment then whatever. If you're reading and writing during the same session, you should be ok, but many times files are used after a process has shut down and is started back up again, in which case the pointers are useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21