View Full Version : The C Programming corner
Lexlimitless 11-07-2005, 08:46 PM This thread is dedicated to those who have done some forms of C who want to share code and help out and those who are doing C and need help and have questions. First question, how much of you know files?? i have another question to ask after that response...
Lexlimitless 11-13-2005, 02:33 PM Can someone examine this code for me and tell me whats wrong? it is reading data from a file and storing into a list
CUSTnode openCusFile(char* text)
{
FILE* cusPtr;
CUSTnode Chead=NULL;//structure of type CUSTnode
CUSTnode current;
//int ch=0;
if((cusPtr=fopen(text, "r"))==NULL) printf("Cannot open file\n");//error message
else{
fseek(cusPtr, SEEK_SET, 0);
fread(current, sizeof(CuNODE),1, cusPtr);
pushCUST(&Chead, current->Cdata);
while(!(feof(cusPtr))){
Chead=Chead->Cnext;
//ch++;
fseek(cusPtr,1+sizeof(CUSTnode), SEEK_CUR);
fread(current, sizeof(CuNODE),1, cusPtr);
pushCUST(&Chead, current->Cdata);
}
fclose(cusPtr);
}
return(Chead);
}
i cant see why its not working... it reads from the file well but when i want to print out what it read it doesnt do that like i read something wrong
death_knight 11-13-2005, 04:46 PM I'm not a c programmer I started on c++, however i see and understand what your trying to accomplish. um your saying that logically everything is being done correctly with the read. could you post your print function and lemme see that, the problem could be in that. oh and use the code tags for source codes not the qoute tags zn :coffee
Lexlimitless 11-14-2005, 01:31 PM I dont see the code tags. anyway what im saying DK is dis: all the other functions and such work as they are supposed to. as in, i went back and fixed all others and they work like they should, but i just didnt fix this function cuz i cant detect the error. its a list so naturally u supposed to have a head pointer that points to a head node rite? i not sure if i did dat or what the hell is wrong i just dont know.
Lexlimitless 11-16-2005, 12:46 AM Okay here is what happens, it says it saved successfully, and it says it read successfully yet i get this when i want to print the data out of it:
http://i22.photobucket.com/albums/b332/krazymental/screen.jpg
here is the code again:
SERPnode openSerFile(char* text)
{
FILE* serPtr;
SERPnode Shead=NULL;
SERPnode current;
if((serPtr=fopen(text, "r"))==NULL) printf("Cannot open file\n");//error message
else{
fseek(serPtr, SEEK_SET, 0);
fread(current, sizeof(serveNODE), 1, serPtr);
pushSERP(&Shead, current->Sdata);
while(!(feof(serPtr))){
Shead=Shead->Snext;
fread(current, sizeof(serveNODE), 1, serPtr);
pushSERP(&Shead, current->Sdata);
}
fclose(serPtr);
}
return Shead;
}
and the print function:
void printSERP(SERPnode* head, char* comp)
{
SERPnode current=*head;
if(*head==NULL) printf("There is no information in list\n");
else if((strcmp((*head)->Sdata.name,comp))==0){
printf(" Service Provider Details Screen \n\n"
" Company Name: %-26s \n"
" Address: %-30s \n"
" Contact Number: %-8s \n"
" CEO Name: %-10s %-15s \n"
" Range of Numbers: %-8s - %-8s \n"
" Registered Customers: %d \n"
" Provision Type: %-4s \n",
(*head)->Sdata.name,
(*head)->Sdata.addrPRO,
(*head)->Sdata.contactnum,
(*head)->Sdata.CEO.fname,(*head)->Sdata.CEO.lname,
(*head)->Sdata.numbers.lowLim,(*head)->Sdata.numbers.upLim,
(*head)->Sdata.regitCus,
(*head)->Sdata.ProvType);
}
else{
current=(*head)->Snext;
while(current!=NULL&&(strcmp(current->Sdata.name,comp))!=0){
current=current->Snext;
}
if(current!=NULL){
printf(" Service Provider Details Screen \n\n"
" Company Name: %-26s \n"
" Address: %-30s \n"
" Contact Number: %-8s \n"
" CEO Name: %-10s %-15s \n"
" Range of Numbers: %-8s - %-8s \n"
" Registered Customers: %d \n"
" Provision Type: %-4s \n",
current->Sdata.name,
current->Sdata.addrPRO,
current->Sdata.contactnum,
current->Sdata.CEO.fname,current->Sdata.CEO.lname,
current->Sdata.numbers.lowLim,current->Sdata.numbers.upLim,
current->Sdata.regitCus,
current->Sdata.ProvType);
}
}
printf("\nPress any key to continue...");
getch();
}
death_knight 11-16-2005, 08:39 PM If its a dumb question you can like put me to shame or whatever but at which point in the code when you read from the file do you assign the particular attibute values of the node stucture from the file to the node?? that is.. name,contact num etc.??? is that even being done?
Lexlimitless 11-16-2005, 09:05 PM here:
pushSERP(&Shead, current->Sdata);
calls the function pushSERP that uses a pointer to a list head node and adds the data of a new node to the head
Lexlimitless 11-21-2005, 01:58 AM Thanks to all who helped but i fixed that problem... I now PWN THIS SECTION PEOPLE so if u need help with ur data structures projects come 2 me.. i hear its due this week....
death_knight 11-21-2005, 02:16 AM post your solution. and explain the source of the problem.
Lexlimitless 11-21-2005, 02:46 AM cool. well its kinda not easy 2 see but here goes. the problems originated in both the read and write functions, so i tried everything and heres what happens:
by nature when u open a file it automatically opens at the beginning of a file.. practical? right. so the fseek was not necessary cuz all id be doin would be to move the file pointer from the start and putting it back to the start (redundant) so scrap that. next thing is i removed the fseek in the loop that moves the pointer forward one structure. why? fread. once u read, fread moves the pointer along 1 structure, so fseek would along with fread would move it twice, really lame stuff. so throw that away. this is all for the read function. next ting is to not prime the loop. see how i had once had an fseek and fread outside and the inside? no need for that, just take out those 2 lines entirely. so what do we have now: (i will put the fixed code and explain the changes)
SERPnode openSerFile(char* text)
{
1. FILE* serPtr;
2. SERPnode Shead=NULL;
3. SERPnode Scurrent;
4.
5. if((serPtr=fopen(text, "r+"))==NULL) printf("Cannot open file\n");//error message
6.
7. else{
8. while(!(feof(serPtr))){
9. Scurrent=(SERPnode)malloc(sizeof(serveNODE));
10. fread(Scurrent, sizeof(serveNODE), 1, serPtr);
11. Scurrent->Snext=Shead;
12. Shead=Scurrent;
13. printf("File read %s\n", Scurrent->Sdata.name);
14. }
15. fclose(serPtr);
16. printf("Opened successfully");
17. }
18. return Shead;
}
okay see there between line 7 and 8? no fseek and fread... cool. we made an adjustment though. there was a pushSERP function that did some stuff, thats a bit 2 complicated for our tastes right about now, so we took that out. instead of having a function that does the list building for us we will do it ourselves.. and we did at lines 9, 11 and 12. each time the code loops a new node will be created called current, it will have memory allocated to it, and then the data is read into it, and the head node Shead is set to point to it (the only problem im having with this now is that is reads 1 record then an giberish.. should read just one)
the write function now:
if(*serv==NULL) printf("No information in list\n");
else{
if((serPtr=fopen("Service Provider.txt", "w+"))==NULL) printf("Could Not open file to save");//error message
else{
Scurrent=*serv;
while(Scurrent!=NULL){
fwrite(Scurrent, sizeof(serveNODE),1, serPtr);
printf("File written %s\n", Scurrent->Sdata.name);
Scurrent=Scurrent->Snext;
}
printf("Saved Successfully\n");
fclose(serPtr);
}
}
i dont tink ive shown u the write function before so explaining really i dont see the necessity, its basically the same thing as the read and it has to be, almost identical for it to work. the above is just a snippet but the read function can be pasted into a text editor for c written into code and compiled, doing dat for the snipped would take alot of writing
|
|