Solved

read line and extract string as integer

Posted on 2004-11-01
180 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
Question by:pancoder
    6 Comments
     
    LVL 5

    Expert Comment

    by:van_dy
    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
    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
    Hi PaulCaswell,
    > you are assuming that an int is 32bits wide.
    Wouldn't make much sense to use 8 digits otherwise ;-)

    Cheers!

    Stefan
    0
     
    LVL 16

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    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 recursion 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.

    934 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now