the above post is no longer valid, ive change the code soo much.
the above post is no longer valid, ive change the code soo much.
ok heres the new porblem.
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.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");
i input text.txt, which exists and has the following text in it.
the output when this is run is:Code:reddas red,da 25/04/1986 4.55
why does it close the program?Code:File opened and has been read. hehe11>
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.
Well it's two of them anyway. No, make that more...Originally Posted by Ubber_C_Noob
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.
You're passing the address of a pointer again, not the pointer itself...Originally Posted by Ubber_C_Noob
No, it actually hasn't been read at this point. Also, wait a second...Originally Posted by Ubber_C_Noob
Yeah, I still don't see ReadCatalogue any place...Originally Posted by quzah
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.
ok, you wanted the function ReadCatatlogue here it is:
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.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); }
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.