I'm basically coding a packet sniffer and no, it's not for any evil intent. Basically, it's supposed to read a packet like this one:
0
10000010000000000000011000101101111110111110000011 0000010000000000000101000011000001
Now the first line represents the message number and the rest of the packet represents the actual message. The beginning and end of each message portion is denoted by the 7 bit string of 1000001. The actual message above is "Boo!" in case you're wondering.
The problem is, it doesn't seem to actually take in the initial 1000001, and it loops infinitely while displaying a converted 'A'.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
FILE* inFile;
char packetNum;
char* chunk;
char* inside;
long int convert;
int main(int argc, char* argv[]){
clrscr();
if (argc < 2){
printf("Insufficient parameters.\n");
printf("\nConvert <filename>\n");
exit(1);
}
inFile = fopen(argv[1], "r");
if (inFile == NULL) {
printf("Unable to open %s. Quitting now.\n", argv[1]);
exit(1);
} else printf("File Name: %s\n", argv[1]);
packetNum = fgetc(inFile);
/*above line takes in the message number from the first line of src*/
printf("Looking for message#: %c\n", packetNum);
chunk = "1000001"; /*had to force it for some reason*/
while (fgets(chunk, 8, inFile) == "1000001"){
/*if it gets 1000001, it starts reading inside via this loop until
it hits 1000001 again*/
while (fgets(inside, 8, inFile) != "1000001"){
/*convert binary string into char and display it*/
convert = strtol(inside, (char**)NULL, 10);
printf("Test: ");
printf("%c\n", convert);
}
printf("-l\n"); /* represents the end of the decoded word */
}
fclose(inFile);
printf("Done.\n");
return(0);
}
Can anyone tell me what went wrong? Any help would be appreciated.