怎样把文本中指定的数据读取出来?该怎么处理
怎样把文本中指定的数据读取出来?
LAT = 11.22, LONG = 33.44, d
WINTER, LMT = 7.0, BUSINESS NOISE
--MEDIAN NOISE VALUES, FA(DB)-- STATISTICAL VALUES IN DB
OVERALL NOISE
FMHZ ATMO GAL MANMADE OVERALL DL DU SL SM SU
15.000 25.2 24.9 44.2 44.3 5.9 9.7 1.5 5.3 1.5
--------------------------------------
文本文件内容入上所示,1、2行根据用户输入能改变,3、4行是不变的,我要把第6行的10个数据读出来赋给10个变量,变量名就是第5行相应的那10个单词,这用C语言该怎么实现?恳请各位高人指点(最好能有程序)
------解决方案--------------------
#include <map>
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
ifstream ifile( "test.txt ");
string line, num, w, n;
int i=0;
map <string, string> test;
map <string, string> ::iterator it;
while(!ifile.eof() && i <5)
{
getline(ifile, line);
i++;
}
getline(ifile, num);
istringstream tline(line), tnum(num);
while(tline> > w)
{
tnum> > n;
test.insert(make_pair(w, n));
}
for(it=test.begin(); it!=test.end(); it++)
cout < <it-> first < < ": " < <it-> second < <endl;
//cout < <test[ "OVERALL "] < <endl; //输出 OVERALL 对应的数值
system( "pause ");
return 0;
}
用 map 保存,(其内部自动排序)
然后你就可以使用 cout < <test[ "OVERALL "] < <endl; 来输出 OVERALL 对应的数值了
【测试文件内容同上】
------解决方案--------------------
//忘记关闭文件了.:)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//struct to hold the data from the file
struct FileData
{
double FMHZ;
double ATMO;
double GAL;
double MANMADE;
double OVERALL;
double DL;
double DU;
double SL;
double SM;
double SU;
LAT = 11.22, LONG = 33.44, d
WINTER, LMT = 7.0, BUSINESS NOISE
--MEDIAN NOISE VALUES, FA(DB)-- STATISTICAL VALUES IN DB
OVERALL NOISE
FMHZ ATMO GAL MANMADE OVERALL DL DU SL SM SU
15.000 25.2 24.9 44.2 44.3 5.9 9.7 1.5 5.3 1.5
--------------------------------------
文本文件内容入上所示,1、2行根据用户输入能改变,3、4行是不变的,我要把第6行的10个数据读出来赋给10个变量,变量名就是第5行相应的那10个单词,这用C语言该怎么实现?恳请各位高人指点(最好能有程序)
------解决方案--------------------
#include <map>
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
ifstream ifile( "test.txt ");
string line, num, w, n;
int i=0;
map <string, string> test;
map <string, string> ::iterator it;
while(!ifile.eof() && i <5)
{
getline(ifile, line);
i++;
}
getline(ifile, num);
istringstream tline(line), tnum(num);
while(tline> > w)
{
tnum> > n;
test.insert(make_pair(w, n));
}
for(it=test.begin(); it!=test.end(); it++)
cout < <it-> first < < ": " < <it-> second < <endl;
//cout < <test[ "OVERALL "] < <endl; //输出 OVERALL 对应的数值
system( "pause ");
return 0;
}
用 map 保存,(其内部自动排序)
然后你就可以使用 cout < <test[ "OVERALL "] < <endl; 来输出 OVERALL 对应的数值了
【测试文件内容同上】
------解决方案--------------------
//忘记关闭文件了.:)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//struct to hold the data from the file
struct FileData
{
double FMHZ;
double ATMO;
double GAL;
double MANMADE;
double OVERALL;
double DL;
double DU;
double SL;
double SM;
double SU;