QuickSorting
I have code below as an example with reading and writing files. The data (fields) are all delimitered with the ':' character. I created a structure and used fscanf to read in the fields from the source file. The program reads in the contents of the source file, then a for loop to skip the '#' comments and then open a new file for output. This new file will contain the sorted data based on the primary field which is the IP address. Is the scanf procedure correctly implemented? People have recommended to me that this is the way but I cant get all of the Ip addresses so that i can do the sorting.
IP Address : Requests : Total Bytes
#
139.132.1.1 : 74184 : 888530078
128.184.2.1 : 44003 : 258043116
#include<stdio.h>
#include<stdlib.h>
struct statistics
{
char name[10];
};
void quicksort(int sort[],int left, int right);
int main(int argc, char *argv[])
{
FILE *fp,*out;
char ch;
int index;
struct statistics st;
char temp[51]; /* length of line */
/* read the filename from the command line and
display an error message if the filename
cannot be opened
*/
if((fp = fopen(argv[1],"r")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
for(index = 0; index < 12; index++)
{
fgets(temp,sizeof(temp), fp);
}
if((out=fopen("webstats.sorted","w")) == NULL)
{
printf("Cannot open file for write");
exit(1);
}
while(fscanf(fp, "%s", st.name) != EOF)
{
fwrite(st.name, sizeof(st), 3, out);
}
fclose(fp); /* close the file */
fclose(out);
return 0;
}
__________________
gbilios
|