1,105,295 Community Members

Reading numbers from a .txt file

Member Avatar
Kitson
Newbie Poster
9 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
iamthwee
Posting Sage
7,036 posts since Aug 2005
Reputation Points: 1,307 [?]
Q&As Helped to Solve: 592 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
1
 

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.

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Yeah, what he said, too.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: