1

I have a file with the following lines. I want to take the date column out and work on the rest three columns. and at the end I want to add the value of 26 and 124, 0 and 65, 584 and 599.

I have already tried

double x,y,z;
ifstream in;
in.open(xyz.txt)
infile >> x >> y >> Z;

I have also tried the get line but it just got me one line of code. If there are 100 different lines, I would like to get and add up all the individual columns except for the date column.

07/15/19 26 0 584

07/15/19 124 65 599

with the code above i am getting exponential form of output

Wyck
  • 5,985
  • 4
  • 34
  • 44
  • Files are a stream of bytes. Any notion of columns is applied at the display end. Texr is often stored in rows to make it easy for the mark I eyeball and human brain to interpret. Unless the data has been stored to be easily read by a machine into columns, the best you can usually do is read the data line by line and stuff it into columns yourself. You may get some inspiration from the way `std::getline` is being used [in this linked answer](https://stackoverflow.com/a/7868998/4581301). – user4581301 Jul 16 '19 at 02:58
  • The line `in.open(xyz.txt)` has two problems. – L. F. Jul 16 '19 at 04:20

1 Answers1

0

To be honest, I do not fully understand the structure of your input file and what you want to achieve. People do really want to help you and therefore I recommend to add more data.

You should also compile your program, before posting. It will show you already there some syntax errors. Without compiling I can tell you that there is

  • Missing semicolon after open function
  • No string in double quotes in open function
  • Capital Z in last statement

All these Bugs the compiler would show you. And, if you want to get help from SO members, you should at least present compiled code . . .

As said. I do not know exactly what you want to do. Anyway, please see below example:

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

int main()
{
    // Open the input file 
    std::ifstream file("yourFileName.txt");

    // We must check, if the file could be opened
    if (file) { 
        std::string oneLine{};

        // Read all lines from file
        while(std::getline(file, oneLine)) {

            // Copy line to a stringstream, so that we can extract the data fields
            std::istringstream issLine(oneLine);

            // Define Variables that will be read from the line
            double x{0.0}, y{0.0}, z{0.0};
            std::string date{};

            // Extract the requested data
            issLine >> date >> x >> y >> z;

            //
            // Do some work with that data
            //
        }
    }
    else {
        std::cerr << "Could not open Input file\n";
    }
    return 0;
}

So first we define a variable file of type ifstream and handover the filename as a parameter for its constructor. This tries to open the file. The file will be closed automatically by the destructor, when the variable goes out of scope.

Then we check that if the file could be opened. This works, becuase the ! operator of the ifstream has been overloaded. If the file could not be opened, then we show an error message.

Next we use the getline function to read a complete line from the file. We will do that in a while loop so that we read one line after the other until we have read all lines in the file.

Now a little bit more tricky. Because we want to extract data, we copy the read line to an istringstream object. From such an object we can extract data.

We then extract all the data and you can do whatever you want with that.

Please look up all functions that I used in the internet and get and understanding. Read a C++Book.

Hope this helps a little.

Armin Montigny
  • 7,879
  • 3
  • 11
  • 29
  • Thank you I solved it. It was a minor error with type mismatch. But I did basically what you did and it solved the problem. – Biplab Wagle Jul 18 '19 at 01:18