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


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

Homework Answers

Answer #1

#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

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 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...
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...
C Programming 1. Write a void function that takes in a 2D array of character and...
C Programming 1. Write a void function that takes in a 2D array of character and have it print out each array on a new numbered line on the console. 2. Illustrate the stack and the heap allocation. Specify what each variable value holds and where different references are pointing to. int main() { int myarray[3]; myfunction (myarray); } int myfunction(int* in) { int i; for (i = 0; i<3; i+= 1) { in[i] = i; } // illustrate the...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT