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:

#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#pragma argsused

using namespace std;

int main(int argc, char* argv[])
{
// Opening the file
ifstream file_in("data.txt");
const int max_char = 15,// Characters expected in header line
header_lines = 1; // Header lines to skip

// Ignore the header lines
for(int j = 0; j < header_lines; ++j)
file_in.ignore(max_char, '\n');

// Set up vectors for each column of data
vector<int> date;
vector<float> maxchl,
sst;

while(!file_to_read.eof()) {
//Set up temporary variables
int dt;
float chl,tmp;

file_in >> dt >>chl >> tmp;

date.push_back(dt);
maxchl.push_back(chl);
sst.push_back(tmp);

}

// Print all the volts read
cout << "\nDate:\n";
for(vector<int>::size_type j = 0; j < date.size(); ++j)
cout << date[j] << '\n';

cout << "\nMaxChl:\n";
for(vector<float>::size_type j = 0; j < maxchl.size(); ++j)
cout << maxchl[j] << '\n';

cout << "\nSST:\n";
for(vector<float>::size_type j = 0; j < sst.size(); ++j)
cout << sst[j] << '\n';

//Pause until a char is entered
char r;
cin >> r;

return 0;
}

Any help would be greatfully appreciated.

Recommended Answers

All 2 Replies

You're not checking if the file was opened successfully, so my guess is that something goes wrong with that.

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).

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.