-2

I am trying to read a .dat file of 11 numbers, the code detects the time incrementation between them(this is an arbitrary value set in the .dat file). I am using while loop to process the other numbers in the fil, but when I use it, only the first value of the file appears on the graphics screen followed by zeros. my code is as follows:

#include <fstream>
#include <iostream>
 #include <string>


using namespace std;

//int k;
double npts;
double time_inc;
//double sensor;
double seismicData;
//double new_double;
ifstream myFile;
ifstream fin;
//ifstream dataOutput;`
int main()
{
    //this takes the data and analyses the number of points as well as the time  incr.
    ifstream fin("SEISMIC.dat", ios::in);
    myFile.open("SEISMIC.dat");
    fin >> npts;
    cout << "Number of data points:    " << npts;
    fin >> time_inc;
    cout << "     Time incrementation:" << time_inc;

    int num;
    //myFile.open("SEISMIC.dat");
    if (!myFile) {
        cout << "Error: file could not be opened" << endl;
        exit(1);
    }
    myFile >> num;
    fin >> num;
    //myFile >> seismicData;
    while (!myFile.eof()) {
        cout << "Next number is:" << num <<endl;
         myFile >> num;
        //cout << "Next number is:" << fin << endl;
        //cout << seismicData << endl;
        //myFile >> seismicData;
    }
    myFile.close();

 }

I'm wondering if anyone could help me out. Attached is a screenshot of the .dat file

Baffer
  • 96
  • 11
  • 3
    My first reaction is, why to you have three file variables and try to open the same file twice? You won't solve a problem by throwing more code at it, fix the code you have. Declare one file variable (preferably in main) and open the file once. That would be a start. – john Feb 06 '23 at 18:05
  • 1
    Please do not repeat the question multiple times. There is a reason why Stackoverflow does not allow a massive code dump, plus one or two sentences of prose, Stackoverflow is not a debugging service. What proof can you show that the file contains "11 numbers"? – Sam Varshavchik Feb 06 '23 at 18:05
  • 1
    My advice is to throw away this code (it's a mess) start again and try to read one number from the file. When you have that easier task working, try to solve the whole problem. – john Feb 06 '23 at 18:09
  • The screenshot is not attached. If it is only numbers, you can upload it as text. – Baffer Feb 06 '23 at 18:17
  • You should read this: [Why is `iostream::eof()` inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Ted Lyngmo Feb 06 '23 at 18:38

1 Answers1

1

Here's some code to read all the numbers from a text file and echo them to the standard output.

#include <fstream>
#include <iostream>

int main()
{
    std::ifstream file("SEISMIC.dat");
    if (!file.is_open())
        std::cerr << "cannot open file\n";
    int num;
    while (file >> num)
        std::cout << num << '\n';
}

As you can see, not much code is needed. However this code will not work if the file is not in the format you described.

john
  • 85,011
  • 4
  • 57
  • 81