Hi, I've had a quick look through this forum and can't really see anything to help. I've got a basic but fair knowldge of C++ but I'm unfortunately not very good at putting my knowledge into raw code :D.

I'm currently doing an assignment to read data from one of four comma separated files and perfom various calculations on each relevant item of data. Each of the four files contain roughly 160,000 entries in the following way -

Thu Feb 01 12:00:00 GMT 1990, 11.317777768
Thu Feb 01 13:00:00 GMT 1990, 10.803333324

The part I'm having difficulty with is reading the number found after the comma, passing it through the relevant code to perform the calculations (I have the code for the calculations running smoothly) and outputting the data gained in a new comma separated file.

I'm using the below code to access the file of choice by the user, but I'm unsure of how to read the data off of the file and pass each one through a loop to calculate the necessary data.

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <Math.h>
using namespace std;

int main()
{

//Trying to get the windspeeds

string myfilename, mystring;

cout << "Enter the name of the Wind Farm\n";
cin >> myfilename;
ifstream inFile;
inFile.open(myfilename.c_str());

if (!inFile){
cout << "Error opening file " << myfilename << endl;
return -1;

}

while(!inFile.eof()) {};

I've already declared a variable, v, to set as the number found after the comma.

Any help / cheeky hints would be grateful. Sorry if I've not layed this out properly also, it's my first time using these forums :$!

Recommended Answers

All 4 Replies

Assuming the lines of the files are all in the same format as the two posted you can use getline() with comma as the delimiter to read the first part of the line and then another call to getline(), or a call to >> to read in the numeral.

Beware, if you mix calls to getline() and >> in the same program it can lead to some "unexpected" results due to the different ways getline() and >> work (most of the problems happen when the default delimiting value of getline() is used, though).

Also, beware that line 26 is a bug waiting to happen. Sooner or later this syntax will result in duplicate reading of the last item in the file.

Here's what Lerner was referring to on line 26 -- feof() and .eof() are identical.

Ah right, cheers guys. Visual Basic is refusing to work on my Pc at the moment for some crazy and bizarre reason, so I'll need to wait untill I'm in Uni tomorrow to batter out some code.

I think I'm getting somewhere on pen and paper, but I won't know untill I plug it in and see if it works :D. I've got a good idea how to go around it now anyway, using getline(), I have no idea why I didn't think to use that atall haha.

For the feof/eof, should I just input an if statement as shown in your link there? I understand that example you provided WaltP, but I'm unsure as to what to put in the if statement found inside the last while, if you understand what I' saying

There were 3 solutions posted. The last one is the best. Just C++ it.

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.