I am having a problem checking whether a file is null or not:
This should work, but microsoft throws a debug assertion failed, I don't understand, any ideas? This is on xp.Code:FILE *fp; if((fp = fopen("test.txt", "w") == NULL)
I am having a problem checking whether a file is null or not:
This should work, but microsoft throws a debug assertion failed, I don't understand, any ideas? This is on xp.Code:FILE *fp; if((fp = fopen("test.txt", "w") == NULL)
you make me rery ascared
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.
>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.
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:
with or without the if(fp != NULL) this error is thrownCode: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; }
Last edited by techrolla; 01-06-2004 at 04:05 PM.
you make me rery ascared
Does the pointer 'editor' point to memory you own?Code:struct editor_stuct { char buffer[BUFSIZE]; } *editor;
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.*
>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.
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.
You do realize that 'editor' is just a pointer, and doesn't actually point at anything, right?Code:struct editor_stuct { char buffer[BUFSIZE]; } *editor;
[edit] Curses. Foiled again. [/edit]
Quzah.
Hope is the first step on the road to disappointment.
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
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
Making something which works with fixed allocations of memory is a lot easier, and the change to dynamic allocation later on is fairly trivial.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; }
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.
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...
is it right to use the sizeof for that?Code:editor_struct *struce; struce = (editor_struct *)malloc(sizeof(editor_struct));
Last edited by techrolla; 01-06-2004 at 04:30 PM.
you make me rery ascared
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.
thanks a lot! I guess Im kindof mixing c++ with c![]()
you make me rery ascared