Hi need a hand with my code as I can get it to sort but it wont sort the right field.
I need it to sort by part_number but only can get it to do customer_number , any ideas would be greatly appreciated to show me where Im going wrong.
Thanks
gstar.
//Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int GetCustomer(FIL E *file,int *number);
int get_field(FILE *file, char delimiter, char *ptr); //This function gets a text field in a 3D matrix
int get_field_float (FILE *file,char delimiter,float *cost);
int compare(const void *a,const void *b);
int _tmain(int argc, _TCHAR* argv[])
{
FILE *file, *output; //two file streams
struct sales
{
int cust_num;
char item[50];
char part_number[15];
float cost;
char date[6];
};
sales mysales[200];
file=fopen("sal es.csv", "r");
//check if file opened
if(file == NULL)
{
printf("can't open file");
return 0;
}
int i=0, j=0;
while (!feof(file))
{
GetCustomer(fil e, &mysales[i].cust_num); //%i
get_field(file, ',',mysales[i].item); //%s
get_field(file, ',',mysales[i].part_number); //%s
get_field_float (file,',', &mysales[i].cost); //%f
get_field(file, ',',mysales[i].date); //%s
i++;
}
qsort(mysales, 196, sizeof(sales), compare); //sort string by sales
printf("\n\n");
for (i=0; i<196; i++)
{
printf("%i;%s;% s;%f;%s \n", mysales[i].cust_num, mysales[i].item, mysales[i].part_number, mysales[i].cost, mysales[i].date);
}
fclose(file);
//***********Outp ut to a file
output = fopen("sorted_s ales.txt", "w+");
fwrite(mysales, sizeof(sales), 196, output);
return 0;
}
// *************** *************** *************** *************** ***********
//
// Functions
//
// *************** *************** *************** *************** ***********
//
// >> This function picks out fields in a delimited file.
// >> You can choose the delimiter to look for.
// >> The function returns the length of the string or -1 for error or EOF
// >>
int GetCustomer(FIL E *file, int *number)
{
int i;
char buffer[80];
// read in customer number
for(i=0;i<=2;i+ +)
{
buffer[i] = fgetc(file);
}
buffer[i] = '\0';
*number = atoi(buffer);
return 0;
}
int get_field(FILE *file, char delimiter, char *ptr)
{
int length=0;
char temp;
while(1)
{
temp=fgetc(file );
if (temp==EOF) return -1;
if((temp==delim iter)||(temp==0 x0A))
{
ptr[length]='\0';
return length;
}
if(temp>0x1F)
{
ptr[length]=temp;
length++;
}
}
return -1;
}
int get_field_float (FILE *file, char delimiter, float *number)
{
char dummy;
int length=0;
char temp[50];
while(1)
{
dummy=fgetc(fil e);
if (dummy==EOF) return -1;
if((dummy==deli miter)||(dummy= =0x0A))
{
temp[length]='\0';
*number=float(a tof(temp));
return length;
}
if(dummy>0x1F)
{
temp[length]=dummy;
length++;
}
}
return -1;
}
/// this function is used by qsort. It has a void pointer entry which is retyped
// inside the function. It is also de-referenced to get at the contents
int compare(const void *a,const void *b)
{
char i= *(int *)a; // this both retypes and dereferences the void pointer
char j= *(int *)b;
if (i==j) return 0;
if (i<j) return -1;
if (i>j) return +1;
}
I need it to sort by part_number but only can get it to do customer_number , any ideas would be greatly appreciated to show me where Im going wrong.
Thanks
gstar.
//Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
int GetCustomer(FIL E *file,int *number);
int get_field(FILE *file, char delimiter, char *ptr); //This function gets a text field in a 3D matrix
int get_field_float (FILE *file,char delimiter,float *cost);
int compare(const void *a,const void *b);
int _tmain(int argc, _TCHAR* argv[])
{
FILE *file, *output; //two file streams
struct sales
{
int cust_num;
char item[50];
char part_number[15];
float cost;
char date[6];
};
sales mysales[200];
file=fopen("sal es.csv", "r");
//check if file opened
if(file == NULL)
{
printf("can't open file");
return 0;
}
int i=0, j=0;
while (!feof(file))
{
GetCustomer(fil e, &mysales[i].cust_num); //%i
get_field(file, ',',mysales[i].item); //%s
get_field(file, ',',mysales[i].part_number); //%s
get_field_float (file,',', &mysales[i].cost); //%f
get_field(file, ',',mysales[i].date); //%s
i++;
}
qsort(mysales, 196, sizeof(sales), compare); //sort string by sales
printf("\n\n");
for (i=0; i<196; i++)
{
printf("%i;%s;% s;%f;%s \n", mysales[i].cust_num, mysales[i].item, mysales[i].part_number, mysales[i].cost, mysales[i].date);
}
fclose(file);
//***********Outp ut to a file
output = fopen("sorted_s ales.txt", "w+");
fwrite(mysales, sizeof(sales), 196, output);
return 0;
}
// *************** *************** *************** *************** ***********
//
// Functions
//
// *************** *************** *************** *************** ***********
//
// >> This function picks out fields in a delimited file.
// >> You can choose the delimiter to look for.
// >> The function returns the length of the string or -1 for error or EOF
// >>
int GetCustomer(FIL E *file, int *number)
{
int i;
char buffer[80];
// read in customer number
for(i=0;i<=2;i+ +)
{
buffer[i] = fgetc(file);
}
buffer[i] = '\0';
*number = atoi(buffer);
return 0;
}
int get_field(FILE *file, char delimiter, char *ptr)
{
int length=0;
char temp;
while(1)
{
temp=fgetc(file );
if (temp==EOF) return -1;
if((temp==delim iter)||(temp==0 x0A))
{
ptr[length]='\0';
return length;
}
if(temp>0x1F)
{
ptr[length]=temp;
length++;
}
}
return -1;
}
int get_field_float (FILE *file, char delimiter, float *number)
{
char dummy;
int length=0;
char temp[50];
while(1)
{
dummy=fgetc(fil e);
if (dummy==EOF) return -1;
if((dummy==deli miter)||(dummy= =0x0A))
{
temp[length]='\0';
*number=float(a tof(temp));
return length;
}
if(dummy>0x1F)
{
temp[length]=dummy;
length++;
}
}
return -1;
}
/// this function is used by qsort. It has a void pointer entry which is retyped
// inside the function. It is also de-referenced to get at the contents
int compare(const void *a,const void *b)
{
char i= *(int *)a; // this both retypes and dereferences the void pointer
char j= *(int *)b;
if (i==j) return 0;
if (i<j) return -1;
if (i>j) return +1;
}
Comment