简体   繁体   中英

Read input Space Separated from the File

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

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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM