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
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 0x78
0x17 0x38 0x58 0x78 0x19 0x30 0x57 0x78
0x16 0x38 0x59 0x79 0x17 0x39 0x56 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 };
char num3[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num4[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num5[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num6[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num7[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num8[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num9[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num10[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num11[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num12[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num13[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
char num14[8] = {0x17, 0x38, 0x58, 0x78, 0x19, 0x30, 0x57, 0x78 };
char num15[8] = {0x16, 0x38, 0x59, 0x79, 0x17, 0x39, 0x56, 0x78};
#include <stdio.h>
int main(void) {
char c; // To store a character read from file
FILE * fp1;
fp1 = fopen("num.txt", "r");
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n') // Increment count if this character is newline
count = count + 1;
char num[count][8];
int i = 0, j = 0, x;
unsigned int data;
FILE * fp;
fp = fopen("num.txt", "r");
int countnum = 0;
while (fscanf(fp, "%x", & data) != -1) //read data from file till end of file is reached
{
if (i < 8) //store first eight elements
{
num[countnum][i++] = data;
}
if (i == 7) {
countnum++;
i = 0;
}
}
for (x = 0; x < countnum; ++x)
{
for (t = 0; t < 8; t++) {
printf("%#x ", num[x][t]);
}
printf("\n");
}
return 0;
}
u can manually set for num1,num2,num3,.... or use a for loop or 2D array if u want to implement it generally, I will help you if u find that difficult, but question wants how to implement this so gave code for that.
Please comment if any doubts, please upvote the solution
Get Answers For Free
Most questions answered within 1 hours.