We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,633 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Reading numbers from a .txt file

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

4
Contributors
4
Replies
19 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Kitson
Newbie Poster
9 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 33

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

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.

Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 8

Yeah, what he said, too.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0692 seconds using 2.71MB