So, I have to create a .c file which sorts out IP addresses in ascending order and gives a count to all ip adresses. Considering there is ip.txt file present while has content like below (IP Address and ErrorCode)
127.0.0.0 500
127.0.0.0 400
127.0.0.1 300
127.0.0.2 100
Please suggest something If you're good with Programming, Advance Thanks!
Below is trial that I performed, Feel free to suggest and modify code for perfect output.
Tried code is attached in Image file, I'm expecting to sort out IP addresses and give count of IP addresses from Max to Min in output console.
File_Operation_Code Below
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char * line = NULL;
size_t len = 0;
ssize_t read;
int count=0, totalCh=0;
FILE *fp = fopen("ip.txt", "r");
if(fp == NULL){
printf("IP File does not exist!\n");
return -1;
}
while ((read = getline(&line, &len, fp)) != -1) {
count++;
totalCh+=read;
printf("line of length: %zu\n", read);
printf("line content: %s", line);
}
printf("Total number of lines=%d\n", count);
printf("Total number of characters=%d\n", totalCh);
// logic to fetch IP addr and sort out and give count to output console pending
fclose(fp);
if (line)
free(line);
return 0;
}
If you write a suitable compare function, then you can just use
qsortto sort the data.But you may not need even this. Below is an example of a way to go from the file to the result in a safe way.
IPv4 address for a host
An IP (v4) address is a group of 4 octets. For a host you can not have
0as the 1st octet or for the last one. Also you can not have all ones ---255.0is the network address and255is the broadcast address.encapsulating: a
Packetfor a valid IP and codeThis is ok for a rapid test and makes life easier. Since this is just toy we can have both the string and the decimal values for the octets.
This function
so_compare()compares 2Packetand return-1ifonecomes beforeotherin sorting order. And+1ifonegoes afterother. This is all you need to sort them usingqsortfrom stdlib inC.And you just need to get one for each valid line in the input file.
validating a line
This can be very simple: we just need a valid IP and a valid code.
sscanf()is good here, since we can parse all 5 values at once.a simple function to parse a line
so_get_packet()below takes a line from the input file and returns a validPacket, in a few lines of code.Extracts the values for the octets, validates them, fills in a new
Packetand we are done.storing valid packets
This is
C. Life is easier with composition and containment. Consider a sequence of packets and we can useVectoras below to store them.So the program is just a matter of
Packetfor each valid line dataVectorthingAs each
Vectorhas asizewe always know the number of addresses we got so far.no need to
qsortthe dataSince we get one line at a time makes more sense to just insert them in order...
No surprise here:
so_insert()as above inserts aPacketinto aVector, in order, so when we reach the end on input all lines are already sorted inVector.consuming a file
This
so_consume_file()does the expected: gets a file name and returns a sorted list of addresses and codes. OrNULLin case of error.And is easy to read: a single loop. If a line has a packet it gets into the list. At the end the list is printed and destroyed.
full code for a test
The program accepts a file name as input. Default is in.txt
example output
Not really tested. Hope it helps as showing a way to go from the raw file data to a prototype in a controlled way.