Question

C Programming Load txt file into char array with eight elements. Each new line will be...

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};

Homework Answers

Answer #1
#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT