Read input Space Separated from the File

Ravi Mehta picture Ravi Mehta · Jun 26, 2016 · Viewed 9.2k times · Source

I have to Work on a Program in C++ Which have to Read Data from .txt File where Data is in this form

DATE TIME DATETIME (Unix time_T Value) MachineID Temperature
now have to take time_T value and Temperature and I need to Perform Radix Sort this file having above 3,00,000 Records each line having 1 Record saved as stated above, I have idea of radix sort but I am totally unaware of splitting above string format in separate queue (time_T, Temp). I'm reading file using below code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    ifstream input("demo.txt");
    string line;    
    while (getline(input, line)) {
        cout << line << '\n';
    }
    return 0;    
}

UPDATE Example of Input 2016-01-01 00:00:04.039251 1451624404 01948 4.9

Answer

Hardik Jain picture Hardik Jain · May 2, 2018

No need to use inFile twice in an iteration. I think this should help:

int main() {
    ifstream inFile("filename.txt");
    int i = 0;

string date,time,datetime;

time_t t1;
float temprature;

while (inFile >> date >> time >> datetime >> t1>> temprature)
{
    cout << t1 << " " << temprature << endl;
}
inFile.close();
return 0;
}