• C

read line and extract string as integer

i want to read a file line by line and extract characters from 15 to 22 i.e. 8 chars to string and make whole string as a integer.The chars in file from 15 to 22 are actually hex integers. how can i do this?
pancoderAsked:
Who is Participating?
 
PaulCaswellCommented:
In that case use:

FILE *fp = fopen(...open your file for reading ..);
if ( fp )
{
  int number;
  while(fscanf(fp, "%*15c%8x%*[^\n]\n", &number)==3){ // You may need to use '==1'
           printf("%d\n", number);
  }
}

Paul
0
 
van_dyCommented:
hello pancoder,

      you should use fgets() to read a whole line from a file at a time. consider the following

FILE *fp = fopen(...open your file for reading ..);
char line[1024];        //assuming each line in the file is no more than 1024 characters.
int number;
while(fgets(line, 1024, fp)){
         sscanf(line + 14, "%x", &number);        // you want to read a number 15th character onwards
                                                                        // %x is used because the number is written in hex in the file
         printf("%d\n", number);
}


hope this helps,
vd
0
 
PaulCaswellCommented:
van_dy's got it. Just a couple of extra suggestions:

FILE *fp = fopen(...open your file for reading ..);
if ( fp )
{
  char line[1024];        //assuming each line in the file is no more than 1024 characters.
  while(fgets(line, sizeof(line), fp)){
           int number;
           sscanf(&line[15-1], "%8x", &number);        // you want to read a number 15th character onwards
                                                                          // %x is used because the number is written in hex in the file
           printf("%d\n", number);
  }
}

But please remember that in the above you are assuming that an int is 32bits wide.

Paul
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
stefan73Commented:
Hi PaulCaswell,
> you are assuming that an int is 32bits wide.
Wouldn't make much sense to use 8 digits otherwise ;-)

Cheers!

Stefan
0
 
PaulCaswellCommented:
Stefan,

Quite right! My point was intended to highlight that this code will not work in a 16bit environment. You would at least have to use "%8lx" and 'long number;' and "%ld\n".

Paul
0
 
pancoderAuthor Commented:
hello ,
               What i want is read a file as a line by line and only read from each line characters 15 to 22 not before 15 and after 22 characters i want?
             how can i implement that in C?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.