Thread: data in a file

  1. #1
    Unregistered
    Guest

    Angry data in a file

    I am trying to open a file and read its content which is:

    1234 0.20
    1256.7 0.45
    345.56 0.29

    each line has two set of numbers separated by a space.

    I know how to open the file

    but I want to use these numbers in calculatuion say the first number is income the second number is tax rate I want to calculate tax amount.

    How can I have the program to take the first number and multiply it by the seconfd number and print the result on the screen
    than do the same for the secon line.

    I tried so many things but I am getting know where so please help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Declare a buffer to read one whole line from the file into and then sscanf the values into the appropriate variables, print the product and get another line:
    Code:
    while ( fgets ( buff, sizeof buff, fp ) != NULL ) {
      if ( sscanf ( buff, "%lf %lf", &a, &b ) == 2 )
        printf ( "%f\n", a * b );
      else
        /* Complain */;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    Thanks prelude...it works......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM