Having Problems with fwrite();

This is a discussion on Having Problems with fwrite(); within the C Programming forums, part of the General Programming Boards category; Hi Guys, i think im doing some mistake in my code, however im not being able to see it. Im ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    5

    Having Problems with fwrite();

    Hi Guys, i think im doing some mistake in my code, however im not being able to see it.

    Im simply trying to save a structure to a text file, altough i check the return value of fwrite, the file is empty when i next look at it heres is part my code:

    Code:
    struct Index
    {	
    	int chave;
    	char ngram[5];
    	int ocorrencias;
    };
    
    int main(void)
    {
        FILE *fp;
        Index *registro;
    
       if((fp = fopen("ngram.txt", "w+")) == NULL) 	
      {
    		printf("O arquivo nao pode ser aberto\n");
    		exit(1);
      }
    Snip...

    Code:
      if(fwrite(registro,sizeof(Index),1,fp)!=1)
      {
              printf("error when writting the file");
      }
      return 0;
    }
    Thanks in advance for any help.
    Last edited by Vash_Calamidade; 02-04-2002 at 05:07 PM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    sorry I don't know how to help you.

    but can you please use code tags and ..snip.. where code is missing eg:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    ..snip...
    Code:
    return 0;
    }
    makes it easier.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    Did as you say Brian, thanks for the tip

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write in binary mode.

    "wb+"

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Unregistered
    Guest
    Hum... Modified the code like you told me to, however the ngram.txt file is still blank and with 0 bytes...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Index *registro;

    I'll assume you're actually allocating something for 'registro' first? If not, with the limited code you've provided, there's nothing more I can do. Be sure you're calling 'fclose( )' when you're done with your file.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    5
    Thanks for the help Quzah after a few more time looking around the code i was able to find the other error point. (One of them was the wb+ option)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 10:34 AM
  3. WriteFile and fwrite
    By George2 in forum C Programming
    Replies: 4
    Last Post: 08-10-2007, 05:33 AM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  5. fwrite problems
    By Newworld in forum C Programming
    Replies: 5
    Last Post: 10-12-2004, 09:03 AM

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