Commit 74552646 authored by omwansafelix's avatar omwansafelix

updated fgets_example with additions of code and test files.

parent 8d6e0585
......@@ -2,16 +2,18 @@
/* fgets example */
// reading text from a file, converting it to hexadecimal ASCII equivalent, then printing output.
#include <stdio.h>
#include <string.h>
int main() {
FILE * pFile;
char testWord[10]; //strings will be stored in these arrays
char defaultIndexes[2]= {'0', 'x'};
unsigned char testWord[10]; //strings will be stored in these arrays
unsigned char defaultIndexes[2]= {"0", "x"};
int i, j, k,l, m, n, len, len2; //looping operators
pFile = fopen ("myfile.txt" , "r");
pFile = fopen ("urlData.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
if ( fgets (testWord , 100 , pFile) != NULL )
......@@ -19,9 +21,9 @@ int main() {
//conversion to hex
len = strlen(testWord);
char mystring[100];
unsigned char mystring[len*2 + 1];
len2= strlen(mystring);
char hexOut[100][4];
unsigned char hexOut[len2][4];
if (testWord[len-1]=='\n')
testWord[--len] = '\0';
......@@ -29,31 +31,37 @@ int main() {
for(i=0; i<len; i++){
//initialise msystring array
sprintf(mystring+i*2 , "%02X", testWord[i]);
hexOut[i][0]=defaultIndexes[0];
hexOut[i][1]=defaultIndexes[1];
// if (i==0)
// k=i;
// hexOut[k][2]=mystring[(k];
// hexOut[k][3]=mystring[k+1];
// k=k+1
} //end of for
}
//print output
printf("Raw hex data of ' %s ' is: \t %s \n", testWord, mystring);
//store output in multidimensional array
for(k=0; k<len2; k++){
hexOut[k][0]=defaultIndexes[0];
hexOut[k][1]=defaultIndexes[1];
hexOut[k][2]=mystring[k];
hexOut[k][3]=mystring[k+1];
}
//output each array element's value
for (m=0; m<len2; m++) {
for (n=0; n<4; n++){
printf("Multidimensional array output of hexOut at [%d][%d] = %d\n", m,n, hexOut[m][n] );
}
for (m=0; m<len2; m++) {
for (n=0; n<4; n++){
printf("Multidimensional array output of hexOut at [%d][%d] = %d\n", m,n, hexOut[m][n] );
}
}
fclose (pFile);
}
}
return 0;
}
} //end of else
return 0;
} //end of function main
//end
//////
[
{
"id": 1,
"name": "46e",
"service": "3",
"account": 12,
"fullname": "46e3e4f0c3f8889d",
"owner": 66
}
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment