[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

read line and extract string as integer

Posted on 2004-11-01
6
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

650 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