Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 25th, 2004, 09:00 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default 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
Reply With Quote
  #2 (permalink)  
Old May 26th, 2004, 12:26 PM
Authorized User
 
Join Date: Apr 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Antony
Default

IP addresses are having 4 nos. Where you are doing the sorting?

I think, you should do fgets and memmove to 4 integers.
you can use longs for the other two.
your structure should be something like this,

int ip1;
int ip2;
int ip3;
int ip4;
long request;
long bytes;

then u do the sorting.
hope u can do it.


Antony
Reply With Quote
  #3 (permalink)  
Old May 27th, 2004, 08:41 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks for the advice. I changed the code in the main and tested the while(fscanf). i write the unsorted data to the new file webstats.sorted. I searched in text and the web for quicksort data stored in files. I found this particular quicksort code from a text. how can i sort the data by the ip address field if the ip has the '.'. Is it the same way as I did it with the ':' delimeter in the fscanf in main?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_ELEMENTS 12123 /* really need to use a counter */

struct IP
{
    char ip[30];
    char requests[20];
    char bytes[30];
}stats;

int main(int argc, char *argv[])
{
        FILE *fp, *out;
    int index;
    char temp[51];
        char c;

        if((fp=fopen(argv[1],"r"))==NULL)
        {
        printf("%d Error: cannot open file\n", argc);
        exit(1);
        }

        if((out=fopen("webstats.sorted","w"))==NULL)
        {
                printf("Error:cannot open file for write.\n");
                exit(1);
        }

          for(index = 0; index < 13; index++)
          {
                fgets(temp, sizeof(temp), fp);
          } /* ignore the comments */

         printf("Writing unsorted data to disk.\n");
   /* c is the ':' delimeter */
     while(fscanf(fp, "%s %c %s %c %s", stats.ip, &c, stats.requests,
         &c, stats.bytes) != EOF)
        {
   /* write to the file out */
    fprintf(out, "%s %c %s %c %s\n", stats.ip, c, stats.requests,
                c, stats.bytes);
       }
                printf("%s %c %s %c %s\n", stats.ip, stats.requests,
                  stats.bytes); /* test - whats in the structure */

                printf("Sorting disk file.\n");
                quick_disk(out, NUM_ELEMENTS);

                fclose(fp); /* close the file */
                fclose(out);

    printf("List sorted.\n");

    return(0);

    }

void quick_disk(FILE *fp, int count)
    {
        qs_disk(fp, 0, count -1);
    }

    void qs_disk(FILE *fp, int left, int right)
    {
        long int i, j;
        char x[100];

        i = left; j = right;

        strcpy(x, getIP(fp, (long)(i+j)/2));

        do
        {
 while((strcmp(getIP(fp,i),x) < 0) && (i < right)) i++;
 while((strcmp(getIP(fp,j),x) > 0) && (j > left)) j--;

            if(i <= j)
            {
                swap_all_fields(fp, i, j);
                i++; j--;
            }
        } while(i <= j);

        if(left < j) qs_disk(fp, left, (int) j);
        if(i < right) qs_disk(fp, (int) i, right);
    }

    void swap_all_fields(FILE *fp, long i, long j)
    {
        char a[sizeof(stats)], b[sizeof(stats)];

        fseek(fp, sizeof(stats)*i, SEEK_SET);
                fread(a, sizeof(stats), 1, fp);

        fseek(fp, sizeof(stats)*j, SEEK_SET);
                fread(b, sizeof(stats), 1, fp);

        fseek(fp, sizeof(stats)*j, SEEK_SET);

                fwrite(a, sizeof(stats), 1, fp);
        fseek(fp, sizeof(stats)*i, SEEK_SET);
                fwrite(b, sizeof(stats), 1, fp);

    }

    char *getIP(FILE *fp, long rec)
    {
        struct IP *ip_address;

        ip_address = &stats;

        fseek(fp, rec*sizeof(stats), SEEK_SET);
        fread(ip_address, sizeof(stats), 1, fp);

        return stats.ip;
    }


gbilios
Reply With Quote









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.