hi....

I need a c++ code that read a log ( Contains numbers and letters) file and compute the average....

This is a sample of the log file....


**********************************************************
Input vector # 99:
OUTPUT: 0 1 0 1 0
**** Vdd = 1.8 **** Tclk = 0.1 ****
SER(FIT): 1.1e-002 1.1e-002 1.9e-002 1.1e-002 5.2e-003

**********************************************************
Input vector # 100:
OUTPUT: 0 1 1 0 0
**** Vdd = 1.8 **** Tclk = 0.1 ****
SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003


I need to compute the horizontal sum of the SER(FIT)values and then compute the average of the sums....

i.e( I want to compute [1.1e-002 + 1.1e-002 +1.9e-002 + 1.1e-002 + 5.2e-003] for each input vector and then compute the average of the sums)...


I can't read the output file and convert the string to a number (since it contains numbers & letters). Instead, I should read the number, then check (using a for loop) where is the position of the letter 'e', and finally construct the number from the string by multiplying the number before the letter 'e' with 10^(number after letter 'e')...

So HOW i could do this?!!

Thanks in advance...

If the format is as follows
SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003
you can parse a line by ignoring the 'SER(FIT): ' and then extracting the floating point values. See below ...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    ifstream in("file.txt");
    string dummy;
    double v1,v2,v3,v4,v5;

    // go through the file parsing the input
    while(in >> dummy)  // skips the string preceding the values
    {
        // try reading in all the values at once
        if(in >> v1 >> v2 >> v3 >> v4 >> v5)
        {
            // output the values 
            cout 
        	<< v1 
        	<< " "
        	<< v2 
        	<< " "
        	<< v3 
        	<< " "
        	<< v4 
        	<< " "
        	<< v5 
        	<< '\n';
        }
        else
        {
            // unexpected: unable to read in 5 values,
            // do some sort of error handling here ...
            break;
        }
    }

    return 0;
}

Note however, that the above code trusts the input a lot, but maybe you get ideas and can start experimenting.

sorry... this code didn't work.... i didn't understand v1,v2 for what? did you mean by v1 vector1 ... or the first value i should read... and how i could calculate the sum and average for a general number of vector.... how could i do the for loop for reading all logvalues...

i will be greatfull if you could help me...

Thanks.....

The above code will read in a file which contains lines such as

SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003
SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003
etc ...

The variables v1 .. v5 (read; v1 = value1, v2 = value2 ..) will hold the floating point values that are read from the file, 5 values per each line.
So for example, if the file test.txt contains the following single line:

SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003

then the output should be:

0.0076 0.013 0.012 0.015 0.0052


So to sum it up, it goes through the whole file, one line at a time expecting 5 floating point values on each line. It does not calculate anything, instead it justs displays the values on screen, using 'cout'.

Thanks it work...

but do you have an idea on how if i want to to calculate the sum of each row of the output... then the average of the sums...

Also do you have an idea on how i could read the file as it is.... because the file im working in is big i can't change it in to just

SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003
SER(FIT): 7.6e-003 1.3e-002 1.2e-002 1.5e-002 5.2e-003
etc...

Thanks alot......

but do you have an idea on how if i want to to calculate the sum of each row of the output... then the average of the sums...

You need more variables, one that counts the lines that are read, and e.g. a double which you can use for calculating a sum and one for storing the average.

Also do you have an idea on how i could read the file as it is....

I think you should be able to read the existing file already now, it works as long as the format of _each_ line is:

<a string> <floating point value> <floating point value> <floating point value> <floating point value> <floating point value>

I.e. string <a string> which cannot be taken as a number, starts a line and is followed by 5 floating point values.

To begin with, add a line counter there and try it with your current file and see how many lines the file contains.

I understand you ... but unfortunately the file im working on is of this format ....

Input vector # 1:
OUTPUT: 1 0 1 0 0
**** Vdd = 1.8 **** Tclk = 0.1 ****
SER(FIT): 1.0e-002 1.5e-002 1.5e-002 1.3e-002 5.2e-003

**********************************************************
Input vector # 2:
OUTPUT: 0 0 1 0 0
**** Vdd = 1.8 **** Tclk = 0.1 ****
SER(FIT): 1.0e-002 1.3e-002 1.3e-002 1.4e-002 3.7e-003

**********************************************************
Input vector # 3:
OUTPUT: 0 1 1 1 0
**** Vdd = 1.8 **** Tclk = 0.1 ****
SER(FIT): 8.5e-003 1.2e-002 1.2e-002 1.3e-002 5.2e-003

**********************************************************
Input vector # 4:


Can you tell me please how i could skip the lines before ...

SER(FIT): 8.5e-003 1.2e-002 1.2e-002 1.3e-002 5.2e-003

Thanks......

OK, here is another version, which filters out all lines that do not start with the string: 'SER(FIT):'. It extracts the values from the string (line) by means of a stringstream object.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main()
{  
  ifstream in("test.txt");
  double v1, v2, v3, v4, v5;
  string dummy, line;
  const string tag = "SER(FIT):";

  // read the whole file through one line at a time
  while(getline(in, line))
  {
    // check does this line start with the tag string 'SER(FIT):' ?
    // note: there must be no tabs/spaces at the start of the line
    if(0 == line.find(tag))
    {
      // yes it does, then use a 
      // stringstream object for extracting the values
      stringstream sstrm(line);

      if(sstrm >> dummy >> v1 >> v2 >> v3 >> v4 >> v5)
      {
        // extraction succeeded, now display the values
        cout 
          << v1 
          << " "
          << v2 
          << " "
          << v3 
          << " "
          << v4 
          << " "
          << v5 
          << '\n';
      }
      else
      {
        // display an error msg and break ...
        cout << "unexpected: unable to extract 5 values, stopping ...\n";
        // break out of the while() loop
        break;
      }
    }
  }

  return 0;
}

That's a starting point for you, for figuring out the calculations and stuff.

This article has been dead for over six months. Start a new discussion instead.