hi,
i have a file which contains
dfca2345f1278aba11110012567ade21
i also have a structure

i need to read from file and print the values of struct in decimal form (i.e s.a=3754566469 , similarly b,c,d,e)
so i ve come up with only this, i am not getting how to populate struct and store something like this(siz.a[]={'d','f','c','a','2','3','4','5','\0'} and print dec value of s.a
i am using snprintf and also strtoul but not getting

#include<stdio.h>
struct s
{
  int a;
  int b;
  short c;
  int d;
  short e
  }siz;
int main()
{
  FILE *fp;
  char line[128];
  char buf[100];
  fp=fopen("a.txt","r");
  if(fp!=NULL)
  {
    while(fgets(line,sizeof(line),file)!=NULL)
    {
      fputs(line,stdout);
     }
     fclose(fp);
 }
 else
 {
  perror(fp);
  }
  return 0;
  }

dfca2345f1278aba11110012567ade21

How do you plan to differentiate between the 5 separate values (according to your structure) in that line?

i am using snprintf and also strtoul

Where?

strtol will allow you to grab numeric values while stepping through a string (although if you include hex the soultion becomes ambiguous).

Line 8 no semicolon after short e.
Line 18, while(fgets(line,sizeof(line),file)!=NULL) file is undeclared. Replace file with fp, as its the file stream.
Line 26, bad pointer for perror(). You may want to go through this.

i corrected it is while(fgets(line,sizeof(line),fp)!=NULL), but i want to read it byte by byte and store in struct variables and ben convert to decimal... any help is appreciated
thanks in advance

You're probably looking for a variation of atoi(). Read a byte, then multiply a running total by the base and add the byte's digit value. Since your digits all appear to be hexadecimal, the base is 16 and some unchecked code for a 4 byte integer might look like this:

#include "ctype.h"
#include "stdio.h"
#include "string.h"

/*
    @description:
        Converts a character into its corresponding decimal 
        value according to the specified numeric base.
*/
int digitvalue(int c, int base)
{
    char *p = "0123456789abcdefghijklmnopqrstuvwxyz";
    char *q = strchr(p, tolower(c));

    if (!q || (q - p) >= base)
        return -1;

    return (int)(q - p);
}

int main(void)
{
    const char *s = "dfca";
    const char *p = s;
    int x = 0;

    while (*p)
        x = 16 * x + digitvalue(*p++, 16);

    printf("The value of '%s' is %d\n", s, x);

    return 0;
}

Now expand that into something that reads from a stream instead of a string, does it multiple times, and works with structure members instead of a temporary variable.

i have given with only structure and hex data, i ve to printf corresponding values of struct variables in decimal,, i am new to c kindly help.. its my assisgnment...

i have given with only structure and hex data, i ve to printf corresponding values of struct variables in decimal,, i am new to c kindly help.. its my assisgnment...

I basically gave you the solution; all you have to do is mold it to suit your specific requirements. If you're not willing to even attempt doing that then I'm not willing to help you any further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.