1

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

Ravi Mehta
  • 454
  • 1
  • 8
  • 20

2 Answers2

4

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;
}
Hardik Jain
  • 156
  • 3
  • 13
-2

Try this...

int main() {
ifstream inFile("filename.txt");
std::string line;
int i = 0;

string date,time,datetime;

time_t t1;
float temprature;

while (std::getline(inFile, line))
{
    inFile >> date >> time >> datetime >> t1>> temprature;
    cout << t1 << " " << temprature<<endl;
}
inFile.close();
return 0;
}
RAVI VAGHELA
  • 877
  • 1
  • 10
  • 12
  • This will discard every odd line as `getline` reads a line and then `inFile >>` also reads a line, but only the second line is used. – DearVolt Oct 27 '17 at 16:03