Hi Daniel,

After you fix the problem mentioned by Alistair you will also need to allocate a buffer to read into. The character pointer sQuote you are passing to fgets is not initialised. You can either call malloc to allocate a buffer on the heap or for this exercise it would be easier use an on stack character array.

A 'segmentation fault' occurs when a process attempts to read or write from / to a region of memory that is not mapped into the process's address space.

cheers
HM


Daniel Fone wrote:


-------------------------------------------------------------
#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







Reply via email to