Reading data from text (Again!)

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 2
Reputation: Beagle is an unknown quantity at this point 
Solved Threads: 0
Beagle Beagle is offline Offline
Newbie Poster

Reading data from text (Again!)

 
0
  #1
Apr 16th, 2008
Hi folks,

I'm just learning C++ (using Borland C++ Builder) and I am having difficulty reading data from a text file created in Excel. I am trying to read the data into vectors which I can then use in a model that I am building.

The problem is that I get no output to the console application. The code that I am using is as follows:

  1. #include <vcl.h>
  2. #pragma hdrstop
  3. #include <iostream>
  4. #include <fstream>
  5. #include <iomanip>
  6. #include <vector>
  7. #pragma argsused
  8.  
  9. using namespace std;
  10.  
  11. int main(int argc, char* argv[])
  12. {
  13. // Opening the file
  14. ifstream file_in("data.txt");
  15. const int max_char = 15,// Characters expected in header line
  16. header_lines = 1; // Header lines to skip
  17.  
  18. // Ignore the header lines
  19. for(int j = 0; j < header_lines; ++j)
  20. file_in.ignore(max_char, '\n');
  21.  
  22. // Set up vectors for each column of data
  23. vector<int> date;
  24. vector<float> maxchl,
  25. sst;
  26.  
  27. while(!file_to_read.eof()) {
  28. //Set up temporary variables
  29. int dt;
  30. float chl,tmp;
  31.  
  32. file_in >> dt >>chl >> tmp;
  33.  
  34. date.push_back(dt);
  35. maxchl.push_back(chl);
  36. sst.push_back(tmp);
  37.  
  38. }
  39.  
  40. // Print all the volts read
  41. cout << "\nDate:\n";
  42. for(vector<int>::size_type j = 0; j < date.size(); ++j)
  43. cout << date[j] << '\n';
  44.  
  45. cout << "\nMaxChl:\n";
  46. for(vector<float>::size_type j = 0; j < maxchl.size(); ++j)
  47. cout << maxchl[j] << '\n';
  48.  
  49. cout << "\nSST:\n";
  50. for(vector<float>::size_type j = 0; j < sst.size(); ++j)
  51. cout << sst[j] << '\n';
  52.  
  53. //Pause until a char is entered
  54. char r;
  55. cin >> r;
  56.  
  57. return 0;
  58. }

Any help would be greatfully appreciated.
Last edited by Narue; Apr 16th, 2008 at 1:03 pm. Reason: Fixed code tags, but there was no formatting to begin with.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,986
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Reading data from text (Again!)

 
0
  #2
Apr 16th, 2008
You're not checking if the file was opened successfully, so my guess is that something goes wrong with that.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: Beagle is an unknown quantity at this point 
Solved Threads: 0
Beagle Beagle is offline Offline
Newbie Poster

Re: Reading data from text (Again!)

 
0
  #3
Apr 20th, 2008
Problem solved, Thank you. It turns out that the data file had to be in the Debug_build folder before it could be found.
(Also there were a couple of other minor bugs that I should have noticed).
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 840 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC