I failed miserably at it when I tried... should i just skip users for time being and try work on other aspects of the project?
Printable View
I failed miserably at it when I tried... should i just skip users for time being and try work on other aspects of the project?
A little advice.Code:int main()
{
char* pUsers;
ReadUsers(&pUsers);
}
void ReasUsers(char** pUsers)
{
/* ... */
*pUsers = malloc(...);
}
Hey again all,
Project fell by the way side after a small incident skiing. 5000ft up, bad slip & trees = !good! Back on my feet sort of again and back to the joys of C... I cant work my previous code propely so im trying a different way. Again all help is both needed and necessary!
I have 1 file called customers.txt
The problem starts when I try include either of the functions:
sort(users,userssize);
userssize=readfromfile(users,linecount);
When I include the sort my file becomes gibberish. When I include the read function it simply crashes.
Regards,
Jer
Code:#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
struct person //creates a struct called person which holds all personal details
{
char name[25];
char address[50];
int idnum;
};
typedef struct person P;
#define customer "customers.txt" //defines various filenames for easy access
#define readwrite "r+w" //defines commands used in fopen()
#define read "r"
#define write "w"
int addusers(P array[],int linecount); //function prototypes
void write2file(P array[],int size);
void sort(P array[],int size);
int readfromfile(P array[],int totalusers);
void main ()
{
int size=10,id=0; //creates counters for array sizes and id is used in binary search
int cntrl=0,linecount=0,linecounter2=0,userssize=0; //creates counters for array sizes
P users[50]; //creates an array of struct type person
do{ //this do loop controls the main menu, it loops around indefinately until 0 is pressed to exit
printf("\t\tWelcome to HBC - Higher Bank Charges\n\n");
printf("\t1. Add New Customer\n");
printf("\t0. Exit Menu\n\n");
printf("Please Enter Your Choice From The Menu\n");
scanf("%d",&cntrl);
if(cntrl==1) //if 1 is pressed it enters the adduser section
{
linecount=0;
//userssize=readfromfile(users,linecount);//reads from the file and puts this data into the array
userssize=addusers(users,userssize);//this adds new users and returns the new size of the array
//sort(users,userssize); //this sorts the array befoe it is written out into a file
write2file(users,userssize);//writes the contents of the customer array to a file
}
else if(cntrl!=0)
{
printf("Invalid Choice,Please Pick Another Number\n");
}
}while(!cntrl==0);
}
int addusers(P array[],int linecount) //this function adds new users
{
int x=0;
int resize;
resize=linecount;
printf("Please enter the user's id number\n");
scanf("%d",&array[x].idnum);
getchar();
printf("Please enter the customers name\n");
gets(array[x].name);
printf("Please enter the customers address\n");
gets(array[x].address);
printf("esteban cambiasso");
linecount=resize;
return linecount;
}
void write2file(P array[],int size)//writes the contents of the array to a file
{
int j=0;
FILE * customerfile = fopen(customer, "a");
if (customerfile==NULL)
{
printf("%s cannot be opened\n",customer);
exit(errno);
}
//for(j;j<size;j++)
//{
//prints array to file
fprintf(customerfile,"%d",array[j].idnum);
fprintf(customerfile,"|%s",array[j].name);
fprintf(customerfile,"|%s\n",array[j].address);
if(feof(customerfile))//break;
//}
fclose(customerfile);///closes connection
}
int readfromfile(P array[],int totalusers)//reads in customer details from file using string token
{
char customerdet[100];//created to read in all customer details as one string
FILE * customers;
char * strpointer = NULL;
int tmpint;
int i = 0;
customers = fopen(customer, read); //opens connection to file
if (customer ==NULL)
{
printf("%s can not be opened\n",customer);
exit(errno);
}
while(!feof(customers))// not at end of file
{
fgets(customerdet,100,customers);//reads in details as one string
//start strtok
strpointer = strtok( customerdet, "|" );
sscanf(strpointer,"%d",&tmpint);
array[totalusers].idnum = tmpint;
while(strpointer != NULL) //as long as pointer doesnt =0 use string token
{
strpointer = strtok( NULL, "/" );
switch(i)
{
case 0:
strcpy(array[totalusers].name,strpointer);//copies part of string pointed at by string token to name
break;
case 1:
strcpy(array[totalusers].address,strpointer);//copies part of string pointed at by string token to address
break;
}
i++;
}
i=0;
totalusers++;
}
fclose(customers);
return totalusers;
}
void sort(P array[],int size)//bubble sort used to sort customer array
{
int k=0;
int j=0;
P hold;
//for(k;k<size;k++)
//{
//for(j;j<(size-1-k);j++)
//{
if(array[j].idnum > array[j+1].idnum)
{
hold = array[j];
array[j]= array[j+1];
array[j+1]=hold;
}
//}
//}
}
while(!feof(customers))
do not use feof to control loop - read FAQ
what if strtok returns NULL? you still try to copy from NULL pointer...Code:while(strpointer != NULL) //as long as pointer doesnt =0 use string token
{
strpointer = strtok( NULL, "/" );
your customer file is delimitered with | why are you strtoking it with "/"?
what about checking the bounds of the customer array?
When i tried to implement the other efof loop it would not accept the "return(0)" for me? Any idea why this is?
To fix copying the null pointer do i just need to implement some way of pointing it to a new funstion?
i changed the line to "|" - that was an typo
what about checking the bounds of the customer array? - I'm not sure what you mean here, sorry. Could you elaborate a small bit more to a complete c idiot!
Yet it seems that your accident has affected your code, as well. You were corrected at least twice before. Do not use void main and do not use gets. It's the devil's tool.
Gets can be the cause of your crashes, too, due to its incredible poor design. Forget that gets ever existed. It should never, ever be used.
That says nothing. If you changed the loop, show the new code.
You need to not copy the string if strtok returns NULL. You can't copy something that doesn't exist, can you? So check if the result is not NULL before trying to do any copying.Quote:
To fix copying the null pointer do i just need to implement some way of pointing it to a new funstion?
It means that perhaps something is writing beyond the end of the customer array. Do you have Visual Studio? Visual Studio can easily catch buffer overrun errors.Quote:
what about checking the bounds of the customer array? - I'm not sure what you mean here, sorry. Could you elaborate a small bit more to a complete c idiot!
Otherwise you're just going to have to do it manually. There are several methods. One could be to create one extra customer object (+1 to the array) and check if that object changes (it shouldn't!).