GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2005, 18:16
wariner wariner is offline
New Member
 
Join Date: Apr 2005
Posts: 3
wariner is on a distinguished road

Help extracting specific data from text file


I am very new to C++ and I have a project involving integration totals.

Please could someone help explain to me how to extract specific data out of a text file:

The way the project works is that I have to take specific data (in this case, acceleration and time values) out of a text file which has been provided. I then need to apply a trapezoidal Riemann sum to the data in order to get velocity and then displacement.

I have got the function for the Riemann sum worked out, all I need help with is how to get the data from the provided file.

It is given in the form:

time1 x-acceleration1 y-acceleration1
time2 x-acceleration2 y-acceleration2
time3 x-acceleration3 y-acceleration3

and is around 7000 lines of that, all numeric.

What I would like to know is, how do I extract the data (and presumably save it to a variable) such as time 1, then time 2 etc. It is all separated by three spaces as shown. I attached a copy of the file, the columns are [time, x-acc, y-acc, x-disp, y-disp]

I would really appreciate if anyone could show me how to pull out the specific data, and then use it in a calculation by way of storing it or using it directly.

Much appreciated
  #2  
Old 23-Apr-2005, 18:28
wariner wariner is offline
New Member
 
Join Date: Apr 2005
Posts: 3
wariner is on a distinguished road

augment


It didn't seem to attach the file last time, so I try again... But it doesn't seem to have worked.
Attached Files
File Type: zip 0406268A_data_p2.zip (61.4 KB, 15 views)
  #3  
Old 23-Apr-2005, 19:16
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi,

Welcome to GIDForums.

Pls read this tutorial. It also has an example to to extract data from file using file I/O.

Thanks,
  #4  
Old 23-Apr-2005, 19:19
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Some ideas:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <vector>

struct data
{
   double time, x_acc, y_acc, x_disp, y_disp;
};

int main()
{
   std::ifstream file("0406268A_data.txt");
   std::vector<data> info;
   data element;
   while ( file >> element.time >> element.x_acc >> element.y_acc
                >> element.x_disp >> element.y_disp )
   {
      info.push_back(element);
   }
   // Example of doing something with the data.
   for (std::vector<data>::size_type i = 0; i < info.size(); ++i)
   {
      std::cout << info[i].time  << ' '
                << info[i].x_acc << ' '
                << info[i].y_acc << std::endl;
   }
   return 0;
}
  #5  
Old 24-Apr-2005, 03:14
wariner wariner is offline
New Member
 
Join Date: Apr 2005
Posts: 3
wariner is on a distinguished road

Many thanks


Thanks so much for the assistance, it really helped me to understand and made my life easier. That tutorial looks great, I'm gonna give it a look now, so thanks to nkhambal.

Much appreciated, Dave Sinkula. The code was grande, it was more helpful than any of my lecturers!

I suppose all I need to do now is apply the formulae to the data, and output it to a CSV file, which should be fine. I guess the text file uploaded after all. I think it didn't work the first time, because I tried to upload from Konqueror, and then in Firefox it seemed to have worked.

Thanks again.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.