首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

怎样把文本中指定的数据读取出来?该怎么处理

2012-03-03 
怎样把文本中指定的数据读取出来?LAT11.22,LONG33.44,dWINTER,LMT7.0,BUSINESSNOISE--MEDIANNOISEVALUE

怎样把文本中指定的数据读取出来?



    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;


};

#define MAX_CHAR_PER_LINE 300

//read a line, and return the first token of the line
int ReadLine(FILE* fp, char* firstToken, char* lineString)
{
char strDel[] = " \t\r\n ";
static char strBuff[MAX_CHAR_PER_LINE]={0};

char* token = NULL;
if(fgets(strBuff, MAX_CHAR_PER_LINE-1, fp)) {
strcpy(lineString, strBuff);
token = strtok(strBuff, strDel);
if(token) sprintf(firstToken, token);
else sprintf(firstToken, " ");
return 1;
}
else {
if(feof(fp)) return EOF;
return 0;
}
}

// read data from the file;
// return true when succeed, otherwise false
bool ReadData(const char* fileName, FileData* data)
{
FILE* fp = fopen(fileName, "r ");
if (NULL==fp) {
printf( "Error: can 't open the file %s ", fileName);
return false;
}

int minCharPerLine = 10;
char firstToken[MAX_CHAR_PER_LINE] = {0};
char lineString[MAX_CHAR_PER_LINE] = {0};

bool readSucceed = false;
while (ReadLine(fp, firstToken, lineString)> 0) {
if (stricmp(firstToken, "FMHZ ")==0) {
while(ReadLine(fp, firstToken, lineString)> 0) {
if (strlen(lineString)> minCharPerLine) {
int numRead = sscanf(lineString, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf ",
&data-> FMHZ, &data-> ATMO, &data-> GAL, &data-> MANMADE, &data-> OVERALL,
&data-> DL, &data-> DU, &data-> SL, &data-> SM, &data-> SU);
if (numRead!=10) {
printf( "Error, the file data is corrupt! ");
fclose(fp);
return false;
}
else {
fclose(fp);
return true;
}
}
}
}
}

fclose(fp);

return readSucceed;
}


//testing the ReadData

int main(int argc, char** argv)
{

//the file to open
char * fileName = "d:\\temp\\test.txt ";

// the data
FileData data;

if (ReadData(fileName, &data)) {
//if succeed, print the value
printf( "\n%g\n%g\n%g\n%g\n%g\n%g\n%g\n%g\n%g\n%g\n ",
data.FMHZ, data.ATMO, data.GAL, data.MANMADE, data.OVERALL,
data.DL, data.DU, data.SL, data.SM, data.SU);
}

system( "pause ");

return 0;
}

热点排行