?
Solved

read line and extract string as integer

Posted on 2004-11-01
6
Medium Priority
?
232 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:pancoder
6 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12470816
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12470885
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
 
LVL 12

Expert Comment

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

Cheers!

Stefan
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12474985
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
 

Author Comment

by:pancoder
ID: 12480567
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
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 200 total points
ID: 12481131
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

Featured Post

SMB Security Just Got a Layer Stronger

WatchGuard acquires Percipient Networks to extend protection to the DNS layer, further increasing the value of Total Security Suite.  Learn more about what this means for you and how you can improve your security with WatchGuard today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question