Hi,
It should be :
if (myfile == NULL) {
otherwise myfile is set to NULL, hence the segmentation fault when you try to read
from it.
Regards,
Alastair
Daniel Fone wrote:
> Hi guys,
>
> I'm not sure how many of you can code out there, but if there are any I have a
> small problem. Here is the source code...
>
> ---------------------------------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
>
> int main()
> {
> FILE *myfile;
> myfile = fopen("quotes", "r");
> if(myfile = NULL)
> {
> printf("Quote file not found!");
> return 1;
> }
> char * sQuote;
> fgets(sQuote, 256, myfile);
> printf(sQuote);
> return 0;
> }
> --------------------------------------------------------------
>
> I then compile the code with "gcc quote.cpp -o quote". quote.cpp is the name
> of the source file, and the file "quotes" is sitting in the same directory as
> the source. The compiler gives no errors but when I try to run it it says
> "Segmentation fault".
> I have no idea what this means and this is the first time I have done any file
> I/O in linux. Does anyone know what this means and/or how to fix it?
>
> Thanks,
>
> Daniel
> --
> ******************************************
> This E-mail was sent using 100% recyclable electrons :-)
> ******************************************