读书人

C++从txt中读取数据,该如何解决

发布时间: 2013-02-17 10:44:46 作者: rapoo

C++从txt中读取数据
本帖最后由 jszjvictor 于 2013-01-28 06:00:06 编辑 请教大家怎么从一个有描述的txt中读取数字,而忽略描述,比如:
//total length l: 1
//total time tpmax:1
//time step interval dt: 1.0e-6
//define cell number n: 100
//choice of boundary conditions(1: DIR; 2: ZGD; 3: PER) B: 1
//choice of convection types(1: UDS; 2: CDS; 3: TVD) C: 1
//switch on diffusion?(diffusion: 1; no diffusion: 0) dif: 0
//switch on convection?(convection: 1; no convection: 0) con: 1
就是说C++需要读取的只有每行末尾:后的的那个数字
[解决办法]
这里是程序:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main(void)
{
const char filename[] = "test.txt";
ifstream fs;
string line;
string temp;
int n = 0;
int num = 0;
stringstream ss;

fs.open(filename);
if(!fs.is_open())
{
cout<<"不能打开文件"<<filename<<endl;
return 1;
}

while (getline(fs, line))
{
ss.str(line);
n = line.rfind(':');
temp = line.substr(n+1);
cout<<temp<<endl;
}

getchar();
return 0;
}

[解决办法]
strrchr

Defined in header <string.h>

char *strrchr( const char *str, int ch );


Finds the last occurrence of the character ch in the byte string pointed to by str.
Parameters
str - pointer to the null-terminated byte string to be analyzed
ch - character to search for
Return value

Pointer to the found character in str, or NULL if no such character is found.
Example

#include <string.h>
#include <stdio.h>

int main()
{
char szSomeFileName[] = "foo/bar/foobar.txt";
char *pLastSlash = strrchr(szSomeFileName, '/');
char *pszBaseName = pLastSlash ? pLastSlash + 1 : szSomeFileName;
printf("Base Name: %s", pszBaseName);
}

Output:

Base Name: foobar.txt


[解决办法]
试试这个呢。

#include<iostream>
#include<fstream>
#include<string>
#include <math.h>
using namespace std;

void fn3(string filename);
double StringToInt(string str);
double SignNumberStringToInt(string str);


double SimpleNumberStringToInt(string str);

int _tmain(int argc, _TCHAR* argv[])
{
fn3("D:\\List.txt");
system("pause");
return 0;
}

//function:read data from file
void fn3(string filename)
{
fstream fs(filename,ios::in
[解决办法]
ios::out);
string string_line;
string string_number;
int index;
double number;
if(!fs.bad())
{
while(getline(fs,string_line))
{
cout<<"每行字符串:"<<endl;
cout<<string_line<<endl;
index=string_line.find_last_of(':',string_line.size());
string_number=string_line.substr(index+1);
string_number.assign(string_number.begin() + string_number.find_first_not_of(' '), string_number.begin() + string_number.find_last_not_of(' ') + 1);
cout<<"最后一个冒号后的字符串(去除空格后):"<<endl;
cout<<string_number<<endl;
number=StringToInt(string_number);
cout<<"转换数字为:"<<endl;
cout<<number<<endl;
string_number="";
}
}
}

double StringToInt(string str)
{
double number;
int e_index,E_index,cout;
cout=str.size();
if(cout==0)
{
return -1;
}
e_index=str.find_first_of('e');
E_index=str.find_first_of('E');
if(e_index!=-1)
{
if(E_index!=-1)
{
//cout<<"cant convert to number"<<endl;
return -1;
}

if(str.find_first_of('e')!=str.find_last_of('e'))
{
//cout<<"cant convert to number"<<endl;
return -1;
}
string str_copy(str);
string left_str=str.erase(e_index,cout-e_index);
string right_str=str_copy.erase(0,e_index+1);
number=SignNumberStringToInt(left_str)*pow(10,SignNumberStringToInt(right_str));
}
else
{
if(E_index!=-1)
{
if(str.find_first_of('E')!=str.find_last_of('E'))
{
//cout<<"cant convert to number"<<endl;
return -1;
}
string str_copy(str);
string left_str=str.erase(E_index,cout-E_index);
string right_str=str_copy.erase(0,E_index+1);
number=SignNumberStringToInt(left_str)*pow(10,SignNumberStringToInt(right_str));
}
else
{
number=SignNumberStringToInt(str);
}
}
return number;
}
double SignNumberStringToInt(string str)
{
switch(str[0])
{
case '+':
str=str.erase(0,1);
return SimpleNumberStringToInt(str);
case '-':
str=str.erase(0,1);
return -SimpleNumberStringToInt(str);
default:
return SimpleNumberStringToInt(str);
}
}
double SimpleNumberStringToInt(string str)
{
int cout=str.size();
int dot_index;
double number=0;
dot_index=str.find_first_of('.');
if(dot_index==-1)
{
for(int i=0;i<cout;i++)
{
if(str[i]<48
[解决办法]
str[i]>57)
{
//cout<<"cant convert to number"<<endl;
return -1;
}
number+=(str[i]-48)*pow(10.0,(double)cout-1.0-i);
}
return number;
}
else
{
for(int i=0;i<dot_index;i++)
{
if(str[i]<48
------解决方案--------------------


str[i]>57)
{
//cout<<"cant convert to number"<<endl;
return -1;
}
number+=(str[i]-48)*pow(10.0,(double)dot_index-1-i);
}
for(int i=dot_index+1;i<cout;i++)
{
if(str[i]<48
[解决办法]
str[i]>57)
{
//cout<<"cant convert to number"<<endl;
return -1;
}
number+=(str[i]-48)*pow(10.0,(double)dot_index-i);
}
return number;
}
}


list.txt文件内容
//total length l: 1
//total time tpmax:1
//time step interval dt: 1.0e-6
//define cell number n: 100
//choice of boundary conditions(1: DIR; 2: ZGD; 3: PER) B: 1
//choice of convection types(1: UDS; 2: CDS; 3: TVD) C: 1
//switch on diffusion?(diffusion: 1; no diffusion: 0) dif: 0
//switch on convection?(convection: 1; no convection: 0) con: 1
[解决办法]
#include <stdio.h>
double l, tpmax, dt;
int n, B, C, dif, con;

#define MAXL 1000
char buf[MAXL];
FILE *f;
char *p;
int h;
int main() {
f=fopen("parameter.txt","r");
if (NULL==f) {
printf("Can not open file parameter.txt\n");
return 1;
}
h=0;
while (1) {
if (NULL==fgets(buf,MAXL,f)) break;
p=strrchr(buf,":");
if (p) {
switch (h) {
case 0:if (1!=sscanf(p+1,"%lf",&l )) printf("read l error!\n"); break;
case 1:if (1!=sscanf(p+1,"%lf",&tpmax)) printf("read tpmax error!\n"); break;
case 2:if (1!=sscanf(p+1,"%lf",&dt )) printf("read dt error!\n"); break;
case 3:if (1!=sscanf(p+1,"%d" ,&n )) printf("read n error!\n"); break;
case 4:if (1!=sscanf(p+1,"%d" ,&B )) printf("read B error!\n"); break;
case 5:if (1!=sscanf(p+1,"%d" ,&C )) printf("read C error!\n"); break;


case 6:if (1!=sscanf(p+1,"%d" ,&dif )) printf("read dif error!\n"); break;
case 7:if (1!=sscanf(p+1,"%d" ,&con )) printf("read con error!\n"); break;
default:break;
}
} else {
printf("Error:line %d %s Can not find :\n",h+1,buf);
}
h++;
}
fclose(f);
printf("l =%lf\n",l );
printf("tpmax=%lf\n",tpmax);
printf("dt =%lf\n",dt );
printf("n =%d\n" ,n );
printf("B =%d\n" ,B );
printf("C =%d\n" ,C );
printf("dif =%d\n" ,dif );
printf("con =%d\n" ,con );
return 0;
}

读书人网 >C++

热点推荐