关于C++的txt文本文件提取问题,请指教!
因为才接触c++不过,最近用到txt文本文件提取的问题,文本信息格式如下:
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.282926 0.000000 18.466503 23.750000
wyz 58.281897 0.000000 18.466503 23.750000
wyz 58.281897 0.000000 18.466503 23.750000
wyz 58.281897 0.000000 18.466503 23.750000
wyz 58.281897 0.000000 18.466503 23.750000
wyz 58.281897 0.000000 18.466503 23.750000
我想提取每行的“58.xxxxxx” 以及 “18.xxxxxx”,我应该怎样来编写,最好哪位朋友有相关例程可以参考,文件很大,大概有十几兆,谢谢啦!!
[解决办法]
- C/C++ code
#include <iostream>#include <fstream>#include <string>using namespace std;int main(int argc,char *argv[]){ fstream file("11.txt"); while(!file.eof()) { string a; double b; double c; double d; double e; file>>a>>b>>c>>d>>e; cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl; } return 0;}
[解决办法]
这么规律的当然fscanf
[解决办法]
不知道你是怎么写的,如果说这些数据是一个结构的对象的话,那写的时候肯定是写结构对象了,读的时候也去读结构对象就行了。文件操作尽量用c的吧,c++的file类不太好用...
- C/C++ code
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
[解决办法]
ifstream is("a.txt");
string textline;
getline(is,textline);
istringstream line(textline);
string number;
ofstream os("b.txt");
while(line >> number)
{
if(number.find("58.",0) != string::npos)
os<<number<<" ";
if(number.find("18.",0) != string::npos)
os<<number<<endl;
}
is.close();
os.close();
[解决办法]
#include <windows.h>
#include <stdio.h>
#define MAX_NUM 260
#define NUM9
int main()
{
char FileName[MAX_NUM] = "1.txt";
char Buffer[MAX_NUM] = "";
char Temp1[MAX_NUM] = "";
char Temp2[MAX_NUM] = "";
char str1[NUM] = "58.";
char str2[NUM] = "18.";
char* Str3 = NULL;
char* Str4 = NULL;
FILE* Soure = NULL;
FILE* Dest = NULL;
int restult1 = 0;
int restult2 = 0;
Soure = fopen(FileName,"r");
if(Soure == NULL)
{
printf("Failed to open File %s\n",FileName);
}
Dest = fopen("b.txt","w");
if (Dest == NULL)
{
printf("Failed to Open File b.txt\n");
}
while((fgets(Buffer,MAX_NUM,Soure) != NULL)&&(!feof(Soure)))
{
Str3 = strstr(Buffer,str1);
Str4 = strstr(Buffer,str2);
if((Str3 == NULL) || (Str4 == NULL))
{
printf("error!\n");
}
restult1 = Str3-Buffer+1;
restult2 = Str4-Buffer+1;
for(unsigned long i = restult1-1;i<(restult1+NUM);i++)
{
Temp1[i] = Buffer[i];
//printf("%s ",Temp1);
}
fprintf(Dest,"%s",Temp1+restult1-1);
for(unsigned long j = restult2-1;j<(restult2+NUM);j++)
{
Temp2[j] = Buffer[j];
}
fprintf(Dest,"%s\n",Temp2+restult2-1);
}
fclose(Soure);
fclose(Dest);
return 0;
}
[解决办法]
- C/C++ code
#include <stdio.h>#include <string>void main( void ){ FILE *streamSrc; FILE *streamDes; string s1; string s2; string s3; string s4; string s5; streamSrc = fopen( "src.txt", "r+" ); streamDes = fopen( "dest.txt", "w+" ); if( streamSrc == NULL || streamDes == NULL ) { printf( "The file fscanf.out was not opened\n" ); } else { while(!feof(streamSrc)) { fscanf( streamSrc, "%s%s%s%s%s", s1.c_str(),s2.c_str(),s3.c_str(),s4.c_str(),s5.c_str() ); fprintf(streamDes, "%s %s %s",s2.c_str(),s4.c_str(),"\n"); } fclose( streamSrc ); fclose( streamDes ); }}
[解决办法]
float f58,f18;
//wyz 58.281897 0.000000 18.466503 23.750000
fscanf(f,"wyz %f %*f %f %*f",&f58,&f18);
//记不得哪位C++大牛在哪本学习C++的书的前言里面说过
//“用C语言1000行源码能完成的工作千万不要用C++重写!”
//更不用说C语言1行源码能完成的工作了!