-5

Hi i am trying to read two characters from a file and want to send it to uint8_t* as hexadecimal . Code:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int file_handling();

int main() 
{
   uint8_t *output ;

   output=file_handling() ;

   printf("\noutput_main -->  %02x",output);

}

int file_handling()
{ 
    uint8_t *output_hand ;

    char c;

    FILE *f_gets = fopen("filename", "r");
    if(f_gets==NULL)
    {
       printf("Please point to a valid key file!\n");
       fclose(f_gets);  
       return 0;
    }
    char str[3];

    if( fgets (str, 3, f_gets)!=NULL ) 
    {
     /* writing content to stdout */
      puts(str);
      output_hand = (uint8_t *)(str); 
      puts(output_hand);    
      printf("\noutput %s --- %02x --> size --> %lu",str,*output_hand,sizeof(*output_hand));
    }
    fclose(f_gets);

    return *output_hand;
}

following is output

we we

output we --- 77 --> size --> 1 output_main -->  65

what i can understand is 77 is ascii for w and 65 is ascii for e

but i want to put "we" which i suppose is a hex in uint8_t *output where is the problem in main ,if i use pointer "*output=file_handling()" instead of just output i get segmentation fault.

How to read value from a file and put it into uint8_t , where file is having hex characters,how fget identifies it as hex or char.

Thanks

file is a text file

ab
fe
ea
ce
1d

Basically uint8_t *output; *output = 0xFA ; it works but i want to read from above file and put it into output variable

Sam A
  • 59
  • 1
  • 2
  • 8
  • There is a lot that is wrong (probably half a dozen things or so) with the code you have written Sam. Perhaps it would be better to show us an example of what is in the file and what you would like your output to be and then we can work from there. – John Szakmeister Jan 23 '18 at 13:04
  • the file contains hex characters – Sam A Jan 23 '18 at 13:24
  • That doesn't really answer the question. Are you just trying to read data from a file and print it to the screen? Are you trying to manipulate the data in some way? If you simply want to print the character rather than the decimal value of it, use `%c` instead of `%s` (which, btw, was wrong to begin with because of a host of other issues some of which are pointed out by Some programmer dude). – John Szakmeister Jan 23 '18 at 13:29
  • @jszakmeister the file contains just characters , i want to read them and put it into uint8_t *output as hex values ,first thing i don't know how to print uint8_t,secondly unint8_t will contain 1 byte of hex characters that is two char , so i am tring to read two characters from the file at a time . – Sam A Jan 23 '18 at 13:32
  • "but i want to put "we" which i suppose is a hex" What is "a hex"? It is a two letter string. If you want to convert from ASCII to integer, then consult your beginner-level C programming book. – Lundin Jan 23 '18 at 13:33
  • The line `output=file_handling() ;` will not even compile on a C compiler. – Lundin Jan 23 '18 at 13:34
  • @Lundin How to read characters from file and put it uint8_t ? – Sam A Jan 23 '18 at 13:35
  • @Lundin I am using gcc on ubuntu and its getting compiled – Sam A Jan 23 '18 at 13:36
  • You need to tell gcc to be a C compiler instead of a Gnu crapiler. This is done by `gcc -std=c11 -pedantic-errors -Wextra -Wall`. As for how to convert from ASCII to integer, do some research and check the hundreds of SO threads already present on that topic. – Lundin Jan 23 '18 at 13:39
  • Try making this into a [mcve], please! <3 – SIGSTACKFAULT Jan 23 '18 at 16:21
  • I got answer on following page first solution :) [https://stackoverflow.com/questions/18693841/read-contents-of-a-file-as-hex-in-c](https://stackoverflow.com/questions/18693841/read-contents-of-a-file-as-hex-in-c) – Sam A Jan 24 '18 at 08:35

1 Answers1

3

You have plenty of problems, here's three of them:

  • You define str to be an array of two characters, which means it can only contain a single-character string. You then call fgets telling it that str it three characters, which means it can and will write out of bounds of your array.

  • You have declared file_handling to return an int value. You return the first character in the array string, and assign that to the pointer variable output in the main function. You then treat this pointer as a single int value.

  • In the main function you pass the pointer output but print it as an int value. There's a mismatch between the format specifier and the argument.

The first and third issues lead to undefined behavior.

John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621