跪求一段小程序!解决方案
跪求一段小程序!
我有两个txt文本文件,文件一存着若干数字编号,文件二存着全部编号对应下的点的x、y、z坐标值。我想调用这两个文件,提取出文件一中编号对应的点的坐标。跪求程序!!急!!
------解决方案--------------------
#include <stdio.h>
#include <string.h>
int main()
{
//声明FILE指针用来指向一个对应的文件
FILE *fp1=NULL;//指向数字编号
FILE *fp2=NULL;//指向对应的x,y,z
//打开流
fp1=fopen("res\\f1.txt","r");//fopen()函数打开由fname指定的文件,并返回一个关联该文件的流。
fp2=fopen("res\\f2.txt","r");//调用失败返回NULL,“r”打开一个用于读取的文本文件“w”创建一个用于写入的文本文件;
if (fp1==NULL)
{
return -1;
}
if(fp2==NULL)
{
return -1;
}
//用来将编号和坐标对应起来
struct Buffer
{
char index[5];
char x[5];
char y[5];
char z[5];
};
Buffer buf[100];//存放读取的数据
memset(buf,0,sizeof(buf));
//
for (int i=0;i<100;++i)
{
if(feof(fp1)||feof(fp2))//feof()读取到文件末尾是返回一个非零值
{
break;
}
fscanf(fp1,"%s",buf[i].index);//"%s"以字符串的方式读取数据,存放在buf[i].index中
fscanf(fp2,"%s%s%s",buf[i].x,buf[i].y,buf[i].z);
}
//转换(将读取的字符串形式的数据转化为整形)
struct Point
{
int index;
int x;
int y;
int z;
};
Point point[100];
memset(point,0,sizeof(point));
for (int i=0;i<100;i++)
{
point[i].index=atoi(buf[i].index);//atoi() 把字符串转换成整型数
point[i].x=atoi(buf[i].x);
point[i].y=atoi(buf[i].y);
point[i].z=atoi(buf[i].z);
}
//关闭流:打开一个文件一定要关闭
if (fp1!=NULL)
{
fclose(fp1);
}
if (fp2!=NULL)
{
fclose(fp2);
}
return 0;
}
//可能会有更好的方法,本人水平有限
//我是新手,写的不好,大家别骂
------解决方案--------------------
没实际编译运行测试。仅供参考:
我有两个txt文本文件,文件一存着若干数字编号,文件二存着全部编号对应下的点的x、y、z坐标值。我想调用这两个文件,提取出文件一中编号对应的点的坐标。跪求程序!!急!!
------解决方案--------------------
#include <stdio.h>
#include <string.h>
int main()
{
//声明FILE指针用来指向一个对应的文件
FILE *fp1=NULL;//指向数字编号
FILE *fp2=NULL;//指向对应的x,y,z
//打开流
fp1=fopen("res\\f1.txt","r");//fopen()函数打开由fname指定的文件,并返回一个关联该文件的流。
fp2=fopen("res\\f2.txt","r");//调用失败返回NULL,“r”打开一个用于读取的文本文件“w”创建一个用于写入的文本文件;
if (fp1==NULL)
{
return -1;
}
if(fp2==NULL)
{
return -1;
}
//用来将编号和坐标对应起来
struct Buffer
{
char index[5];
char x[5];
char y[5];
char z[5];
};
Buffer buf[100];//存放读取的数据
memset(buf,0,sizeof(buf));
//
for (int i=0;i<100;++i)
{
if(feof(fp1)||feof(fp2))//feof()读取到文件末尾是返回一个非零值
{
break;
}
fscanf(fp1,"%s",buf[i].index);//"%s"以字符串的方式读取数据,存放在buf[i].index中
fscanf(fp2,"%s%s%s",buf[i].x,buf[i].y,buf[i].z);
}
//转换(将读取的字符串形式的数据转化为整形)
struct Point
{
int index;
int x;
int y;
int z;
};
Point point[100];
memset(point,0,sizeof(point));
for (int i=0;i<100;i++)
{
point[i].index=atoi(buf[i].index);//atoi() 把字符串转换成整型数
point[i].x=atoi(buf[i].x);
point[i].y=atoi(buf[i].y);
point[i].z=atoi(buf[i].z);
}
//关闭流:打开一个文件一定要关闭
if (fp1!=NULL)
{
fclose(fp1);
}
if (fp2!=NULL)
{
fclose(fp2);
}
return 0;
}
//可能会有更好的方法,本人水平有限
//我是新手,写的不好,大家别骂
------解决方案--------------------
没实际编译运行测试。仅供参考:
- C# code
//文件1: //编号12 23 34; //文件2: //编号 x y z //1 3.45 4.2 3.6 //2 2.45 4.2 3.6 // ........ //277 4.00 8.38 9.92 //278 5.00 8.38 9.92 #include <stdio.h> FILE *f1,*f2; char ln[80]; int r,n,n2,n21; float x,y,z; int xyz_of_line(int nn,float *xx,float *yy,float *zz) { n2=-1;//跳过第一行:编号 x y z rewind(f2); while (1) { if (NULL==fgets(ln,80,f2)) break; n2++; if (n2==nn) { r=sscanf(ln,"%d%f%f%f",&n21,xx,yy,zz); if (4==r) { if (n21==nn) { return 1; } else { printf("WARNING:编号 error in line %4d:%s",nn,ln); return 1; } } else { printf("ERROR:Format error in line %4d:%s",nn,ln); return 0; } } } return 0; } void main() { f1=fopen("文件1","r"); if (NULL==f1) { printf("ERROR:fopen 文件1 error!\n"); return; } f2=fopen("文件2","r"); if (NULL==f2) { fclose(f1); printf("ERROR:fopen 文件2 error!\n"); return; } while (1) { r=fscanf(f1,"%d",&n); if (1==r) { r=xyz_of_line(n,&x,&y,&z); if (1==r) { printf("%4d:x=%g,y=%g,z=%g\n",n,x,y,z); } else { printf("ERROR:Can not read line %4d!\n",n); } } else if (0==r) { fgetc(f1); } else break; } fclose(f2); fclose(f1); }