Hi Guys,

I'm really struggling getting to grips with C++ I need to read a .txt file which contains something similar to this:

Eng.Speed Torque 
1000.0, 34.55
3800.0, 46.58
6600.0, 47.44
9400.0, 48.75
12200.0, 39.79
15000.0, 24.61

I'm trying to:

.Open a file named torque1.txt
.extract the data
.I want to do something with the data (integrate using the trapezium rule)
.open torque2.txt and do the same
.open torque3.txt ...

I'll be able to write the trapezium rule bit, I'm just struggling to find out how to read numbers with decimals from a text file (I only have limited experience in python).

Is it possible to extract each number individually and store them in two arrays, eng.speed and torque.

This is my code so far:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

string getcontent;
string filename;

int main()
{
    for(int i = 1; i < 10; i++)                // Produces file name torque(number).txt
    {                                          // allowing looping
        stringstream ss;
        ss<< i;
        filename = "torque" + ss.str() + ".txt";
        
        ifstream openfile (filename);           // Opens file to read
        if(openfile.is_open())
        {
            while(! openfile.eof())
            {
                ****Not sure how to read the data in****
            }
        }

        ****Do Something With Arrays****

    }
    cin.get();
}

I know I haven't tried to read the data in here, but everything I'm doing is wrong and I'm just hitting my head against a wall searching the internet.

Hopefully someone can help!

Thanks Kitson

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

You've come to the right place then.

You want to parse the file...

Looking at it, sstream is good as it parses a file by whitespace, so you could use this, and chop out the trailing comma which will be in array[0], yes arrays start at zero.

In that case the easiest way to do this is to:

1) Get the contents of the first array which will be X.XXXX,
2) Find the length of this string and substr the contents from start [0] to the [end -1]. (this should remove the comma)
3) Convert this string to a double - again using stringstreams, then store this in an array or vector.

I know I haven't tried to read the data in here, but everything I'm doing is wrong and I'm just hitting my head against a wall searching the internet.

If you haven't tried to read the data, you haven't done anything wrong -- yet!

And until you actually attempt to read the data, we can't help you fix what you did wrong.

filename has been declared using type string. stream objects need null terminated strings to associate with a file, so you need to adjust the filename by calling the c_str() method in order to open the file.

using the return value of eof() can lead to bug. If you don't try getting the first value from the file before calling eof() I'd recommend you use the approach of using the return value of the stream input method to control the loop. So instead of:

while(! openfile.eof())

I'd do something like:

while(openfile >> somevariable)

or

while(openfile.getline(somevariable))

etc.

commented: Spot on. +15

Yeah, what he said, too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.