简体   繁体   English

读取与文件分离的输入空间

[英]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 我必须使用C ++编写一个程序,该程序必须从.txt文件中读取数据,其中数据采用这种形式

DATE TIME DATETIME (Unix time_T Value) MachineID Temperature DATE TIME DATETIME(Unix time_T Value)MachineID温度
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). 现在必须采取time_T值和温度,我需要执行基数排序此文件有3,00,000以上记录每行记录如上所述1记录,我有基数排序的想法,但我完全不知道拆分上面的字符串格式单独的队列(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 更新输入示例2016-01-01 00:00:04.039251 1451624404 01948 4.9

No need to use inFile twice in an iteration. 无需在迭代中使用inFile两次。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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