C Programming
Load txt file into char array with eight elements. Each new line will be a different array with eight new elements
num.txt is written
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
should result into
char num[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num2[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
#include <stdio.h>
int main(void) {
FILE *fp;
fp=fopen("num.txt","r");
char num1[8],num2[8];
int i=0,j=0,x;
unsigned int data;
while(fscanf(fp,"%x",&data)!=-1) //read data from file till end of file is reached
{
if(i<8) //store first eight elements
num1[i++]=data;
else
num2[j++]=data; //stores next eight elements
}
for(x=0;x<i;++x)
printf("%#x ",num1[x]);
printf("\n");
for(x=0;x<j;++x)
printf("%#x ",num2[x]);
return 0;
}
num.txt
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
Output
Get Answers For Free
Most questions answered within 1 hours.