Thread: FILE pointer null checking problem

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    35

    FILE pointer null checking problem

    I am having a problem checking whether a file is null or not:
    Code:
    FILE *fp;
    if((fp = fopen("test.txt", "w") == NULL)
    This should work, but microsoft throws a debug assertion failed, I don't understand, any ideas? This is on xp.
    you make me rery ascared

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, your () are in the wrong place

    fp = fopen("test.txt", "w") == NULL
    is the same as
    fp = ( fopen("test.txt", "w") == NULL )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yeah, your () are in the wrong place
    More like mismatched. The OP is missing one:
    Code:
    if((fp = fopen("test.txt", "w")) == NULL)
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    This missing ) was just a copy paste error. I still get the problem. Its a runtime error, here is most of the text from the microsoft visual c++ error dialog:

    Debug assertion error!

    .... some file info here...
    file: fgets.c
    line: 59

    Expression: string != NULL

    *************
    this is when I run the program with a text file to load. If you want me to post the main functions used, I can. Thanks.

    EDIT: Actually, the error, after some commenting out, seems to be coming from this function:

    Code:
    struct editor_stuct {
    	char buffer[BUFSIZE];
    } *editor;
    
    
    int openFile(FILE *fp)
    {
    	if(fp != NULL)
    	{
    		while(fgets(editor->buffer, BUFSIZE - 1, fp)) 
    		{ 
    			printf("%s", editor->buffer); 
    		}
    		return FILE_SUCCESS;
    	}
    
    	return FILE_ERROR;
    }
    with or without the if(fp != NULL) this error is thrown
    Last edited by techrolla; 01-06-2004 at 04:05 PM.
    you make me rery ascared

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    struct editor_stuct {
    	char buffer[BUFSIZE];
    } *editor;
    Does the pointer 'editor' point to memory you own?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while(fgets(editor->buffer, BUFSIZE - 1, fp))
    Is editor pointing to a valid editor_struct? Also, you don't need to pass BUFSIZE - 1 (if you were doing it with the nul terminator in mind), fgets handles that kind of thing for you.
    My best code is written with the delete key.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ah, so this is one of those "post one line at a time" jobs where we get to guess at each stage all the possible things you could have screwed up

    It seems that this time you've failed to allocate any memory for your editor pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct editor_stuct {
    	char buffer[BUFSIZE];
    } *editor;
    You do realize that 'editor' is just a pointer, and doesn't actually point at anything, right?

    [edit] Curses. Foiled again. [/edit]

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

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Okay, I guess the problem is the editor pointer. I made buffer a local variable and it worked, but how could I make it so I owned it? Would I use memalloc? I have to admit, this is my first attempt with c i/o or c in general.
    you make me rery ascared

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well your best bet is to leave all those * and -> operators out of it for the moment and go with memory which the compiler allocates for you.

    So
    Code:
    struct editor_stuct {
    	char buffer[BUFSIZE];
    } editor;
    
    
    int openFile(FILE *fp)
    {
    	if(fp != NULL)
    	{
    		while(fgets(editor.buffer, BUFSIZE - 1, fp)) 
    		{ 
    			printf("%s", editor.buffer); 
    		}
    		return FILE_SUCCESS;
    	}
    
    	return FILE_ERROR;
    }
    Making something which works with fixed allocations of memory is a lot easier, and the change to dynamic allocation later on is fairly trivial.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    thanks, but what if I want to use a pointer for editor? malloc()? something like this:
    *editor = (editor_struct *)malloc(sizeof(editor_struct)); that doesnt work I know, but its something I guess.

    Okay, Ive edited this post like 5 times and I finally got it, please ignore the above...
    Code:
    	editor_struct *struce;
    	struce = (editor_struct *)malloc(sizeof(editor_struct));
    is it right to use the sizeof for that?
    Last edited by techrolla; 01-06-2004 at 04:30 PM.
    you make me rery ascared

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    In C, we don't cast the return result of malloc (see the FAQ, many previous posts)

    struce = malloc(sizeof(editor_struct));

    Whilst your use of sizeof is correct, there is a better way which is a bit less effort, and is slightly more maintainable in the long run

    struce = malloc( sizeof *struce );
    If the type of the pointer changes, the malloc call automatically adapts without you having to change any code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    35

    Red face

    thanks a lot! I guess Im kindof mixing c++ with c
    you make me rery ascared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM