Thread: Segmentation faults out the ****

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    the above post is no longer valid, ive change the code soo much.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    ok heres the new porblem.

    Code:
    char*Catalogue = (char*)malloc(sizeof(char*)*10);
    scanf("%s",&Catalogue);
    if((myfile = fopen(&Catalogue,"r+"))==NULL)
    {
       printf("\nFile could not be loaded.\n");
    }
    else
    {
       printf("\nFile opened and has been read.\n");
       ReadCatalogue(myfile);
       fclose(myfile);
    }
    printf("hehe");
    this is inside a loop whos condition is independant of the file useage, it is controled by what number a user selects earlier in the program and thus should not be affected by the opening of the file.

    i input text.txt, which exists and has the following text in it.

    Code:
    reddas
    red,da
    25/04/1986
    4.55
    the output when this is run is:

    Code:
    File opened and has been read.
    hehe11>
    why does it close the program?

    it should return to the top of the loop and ask me to select another option.

    please help, i know that it calls ReadCatalogue, reads the stuff from the .txt file and returns to main, but it quits on me and that isnt acceptable.
    Last edited by Ubber_C_Noob; 09-11-2005 at 12:03 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ubber_C_Noob
    ok heres the new porblem.
    Code:
    char*Catalogue = (char*)malloc(sizeof(char*)*10);
    scanf("%s",&Catalogue);
    Well it's two of them anyway. No, make that more...

    1) Catalogue is a pointer to a character. You allocate 10 pointers to characters, not 10 characters.
    2) You typecast malloc. Which if this was right, would be wrong. So really you end up allocating space for 10 pointers to characters, but you treat them as characters. I suppose technically you and get away with that, but it's still wrong.
    3) You don't use & when you already have a pointer that you're using with scanf. You're passing it the address of a pointer, not a pointer.
    Quote Originally Posted by Ubber_C_Noob
    Code:
    if((myfile = fopen(&Catalogue,"r+"))==NULL)
    {
       printf("\nFile could not be loaded.\n");
    }
    You're passing the address of a pointer again, not the pointer itself...
    Quote Originally Posted by Ubber_C_Noob
    Code:
    else
    {
       printf("\nFile opened and has been read.\n");
       ReadCatalogue(myfile);
       fclose(myfile);
    }
    printf("hehe");
    No, it actually hasn't been read at this point. Also, wait a second...

    Quote Originally Posted by quzah
    So show us the ReadCatalogue function, and not just your call to it.
    Yeah, I still don't see ReadCatalogue any place...


    Quzah.
    [edit] Beaten twice. This is what happens when you harvest felled high plains arbor while you type... [/edit]
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    ok, you wanted the function ReadCatatlogue here it is:

    Code:
    void ReadCatalogue(FILE *myfile)
    {
       char*loadedfile=(char*)malloc(sizeof(char*)*600);
       char*temp = (char*)malloc(sizeof(char*)*20);
       while(fgets(temp,sizeof(temp),myfile))
       {
          strcat(loadedfile,temp);
       }
       printf(loadedfile);
    }
    this i know is correct and i know that the file has been read cause i get a print out of the file before the thing quits on me.

    that printf() statment above is supposed to tell the user that the file has been opened and read only if it was able to be opened, since were only useing txt files i dont see how you can open the file and not be able to read it in the future.
    Last edited by Ubber_C_Noob; 09-11-2005 at 12:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  2. oldiofclose.c and segmentation faults
    By sd_padilla in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 02:24 PM
  3. Segmentation faults on Linked Lists. (Please help!!)
    By summerrainx in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 07:23 AM
  4. segmentation faults pervade
    By ezwise in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:17 PM
  5. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM