读书人

求高手帮忙修改小程序!解决思路

发布时间: 2012-02-25 10:01:49 作者: rapoo

求高手帮忙修改小程序!
我有两个txt文本文件,文件一存着若干数字编号,文件二存着全部编号对应下的点的x、y、z坐标值,小数点后保留5位数字。我想调用这两个文件,提取出文件一中编号对应的点的坐标。以下程序无法显示结果,求高手修改!!急!!
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <set>
using namespace std;
#include <stdio.h>
#include <string.h>


int main()
{
//声明FILE指针用来指向一个对应的文件
FILE *fp1=NULL;//指向数字编号
FILE *fp2=NULL;//指向对应的x,y,z
//打开流
fp1=fopen("out.txt","r");//fopen()函数打开由fname指定的文件,并返回一个关联该文件的流。
fp2=fopen("jiedianzuobiao.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);
}
cout<<buf[i].index;
//关闭流:打开一个文件一定要关闭
if (fp1!=NULL)
{
fclose(fp1);
}
if (fp2!=NULL)
{
fclose(fp2);
}
return 0;
}

[解决办法]
我是个初学者 是说下我的感觉呵呵 我的out.txt 文件如下:
11111
22222
33333
44444
55555
jiedianzuobiao.txt内容如下:
11 22 33
11 22 33
11 66 99
88 66 88
99 66 55
其实你的程序具体我没有改只是加了两句 ,其中的某一部分变成:
for (int i=0;i<100;++i)
{
if(feof(fp1)||feof(fp2))//feof()读取到文件末尾是返回一个非零值
{
break;
}
fscanf(fp1,"%s",buf[i].index);//"%s"以字符串的方式读取数据,存放在buf[i].index中
cout << buf[i].index << " ";//新加的显示
fscanf(fp2,"%s%s%s",buf[i].x,buf[i].y,buf[i].z);
cout << buf[i].x << " " << buf[i].y << " " <<buf[i].z << " " << endl;//新加的显示
}
system("pause");//让系统在此处停止 应该也就是文件暂时不关闭而已 像楼上有人说的一样 执行之后 可以看到你的结果 正向楼上的人所说的 cout输出 不过不知道 这样子可以不? 希望对你有用。。。。
[解决办法]
没实际编译运行测试。仅供参考:

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    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);} 


[解决办法]
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
whlie (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
whlie (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。

读书人网 >C++

热点推荐