24小时热门版块排行榜    

CyRhmU.jpeg
查看: 2369  |  回复: 6
本帖产生 2 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

dp.xiong

新虫 (初入文坛)

[交流] c语言高手请进,请勿灌水,谢谢!(关于读写文件的问题)已有3人参与

CODE:
#include
#include

double featurestrain[20000][100];
int numbertrain[20000][100];
int labeltrain[20000];

int main()
{
     int TR=0,i; //TR表示文件数据的行
   FILE *fpi1;
     fpi1 = fopen(".\\bn_pn_train1.scale","r" );
     while(1)
    {

         if (fscanf(fpi1,"%d",&labeltrain[TR]) == EOF ) break;
         for(i=0;i<94;i++)
               fscanf(fpi1,"%d:%lf",&numbertrain[TR][i],&featurestrain[TR][i]);
         printf("%d\n",labeltrain[TR]);
         for(i=0;i<94;i++)
               printf("%d:%g\n",numbertrain[TR][i],featurestrain[TR][i]);
         TR++;
    }
    printf("%d",TR);
    return 0;
}

文件中的一行数据格式如下   
1 1:-0.392523 2:-0.795059 3:0.127944 4:-0.285647 5:-0.542299 6:-0.34472 7:-0.723648 8:-1 9:-0.427386 10:-0.499505 11:-1 12:-0.412413 13:-0.0579479 14:-0.0481203 15:-0.541667 16:-0.589627 17:-0.532492 18:-0.0713881 19:0.785235 20:0.0437904 21:0.46865 22:-0.73356 23:-0.683265 24:-0.55742 25:0.13721 26:0.290154 27:-0.598256 28:-0.543763 29:0.226488 30:0.653741 31:-0.0792884 32:0.0308415 33:-0.818612 34:-0.747322 35:-0.357336 36:0.0230279 37:-0.452742 38:-0.00194553 39:-0.619813 40:-0.0350584 41:-0.277336 42:0.1875 43:-0.248824 44:-0.4975 45:-0.0756125 46:-0.0342379 47:0.780875 48:0.651629 49:-0.765079 50:-0.697386 51:-0.668103 52:-0.822978 53:-0.793103 54:0.217391 55:-0.376312 56:-0.570292 57:0.5 58:-0.628571 59:-0.507122 60:-0.468966 61:-0.714746 62:-0.684013 63:0.00427309 64:-0.444447 65:-0.834313 66:-0.73717 67:-0.501831 68:-1 69:-0.544872 70:-0.848853 71:-0.282048 72:-0.316231 73:-1 74:-0.766382 75:-1 76:-1 77:-1 78:-0.735382 79:-0.187289 80:-0.684611 81:-1 82:-0.384608 83:-0.57265 84:-1 85:-0.769222 86:-0.907689 87:0.148717 88:-1 89:-0.876315 90:-0.519233 91:-0.601139 92:-1 93:-0.179477 94:-0.809019  
这表示的是一行数据,总共有64行这样的数据。运行上面的代码,只能读取前边的63行,增加到64行就出现一直是0的情况,且出现无限循环,如果减少到63行,就可以正常运行,数据格式完全一样且没有错误,思索好久,不能解决,急寻高手高高手解答,谢谢!
不是动态分配内存的问题,我用动态分配内存的方法试了,结果也是这种情况。
我用的vc6.0


有兴趣的朋友可以看一下我上传的数据文件,谢谢![ Last edited by 微尘、梦想 on 2011-6-29 at 22:31 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dp.xiong

新虫 (初入文坛)


dubo(金币+1): 欢迎常来程序语言版讨论 2011-06-30 18:44:41
引用回帖:
Originally posted by dubo at 2011-06-29 23:20:02:
如果行数固定,建议用for()
你也可以仿照这个改改
[code]// reaMNdb.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <stdio.h>
#include < ...

编程果然厉害呀,非常感谢,我得好好看看了,呵呵
6楼2011-06-30 15:17:11
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 7 个回答

dubo

金虫 (著名写手)

优秀版主

★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
余泽成(金币+3, 程序强帖+1): 谢谢参与应助! 2011-06-30 17:56:27
如果行数固定,建议用for()
你也可以仿照这个改改
CODE:
// reaMNdb.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include "iostream"
using namespace std;
#define natoms 200000
#define FILENAME1 water.gro
#define TRAJ_LEN 501
#define MN 12
#define cloumn_num  4
//=======define==========

struct grofile
{
        int totalatom;
        char title[100];
        int residuenr[natoms];
        char residuename[natoms][6];
        char atomname[natoms][6];
        int atomnr[natoms];
        float x[natoms],y[natoms],z[natoms],vx[natoms],vy[natoms],vz[natoms];
        float box_x,box_y,box_z;

};
struct pdbfile
{

        char title[100];
        char remark[1000];
        char cristal[1000];
        char mode_l[100];
        //int residuenr[natoms];
        char residuename[natoms][7];
        int atomnr[natoms];
        char atomname[natoms][5];
        char atomname2[natoms][4];
        int one[natoms];
        float x[natoms],y[natoms],z[natoms],dianhe[natoms],zero[natoms],vx[natoms],vy[natoms],vz[natoms];
        //        float box_x,box_y,box_z;
    char te_r[100];
        char endmd_l[100];
};
struct xvgfile
{
        float string[natoms][cloumn_num];
};
//=========struct============
//pdbfile oldfile,newfile;
xvgfile oldfile,newfile;
//grofile oldfile,newfile;
char *atom1="W\0 ";//the atom we will label
char *atom2="W\0";
float vdwd;//vandeva radio
bool label_water[natoms];//true overlapping
bool flag_vec=00;//judge if vec is effective
int ndx1[10000],ndx2[10000];//this is the index array for calc
float buffer[1000],buffer2[10000];//buffer store the distant average
int total_line=0;
int head_line=0;
int line;
//float work[100000][3];//temp_work array
//=========global val=========



void totalline(FILE *fp)
{
        char temp_str[500];
                //fgets(oldfile.title,100,fp);//title
        //int totalline=0;
                for (int i=0;1;i++)
                  { if(fgets(temp_str,1000,fp)==NULL)
                      break;
                          total_line++;
                   }
                       
                //return totalline;
       
}
void headline(FILE *fp)
{
        char temp_str[500];
        char a,b=' ';
        head_line=0;
                        //fgets(oldfile.title,100,fp);//title
                for (int i=0;1;i++)
                  {
                          fgets(temp_str,1000,fp);
                          a=temp_str[0];
                          //if(strcmp(a,b)==0)   
                          if(a-b==0)   
                      break;
                          
                          head_line++;
                   }
          //  return totalline;
       
}
void readxvg(FILE *fp)
{
            //int tt=oldfile.totalatom;
        int i=0;
        char temp_str[500];
            for (;i                 {   
                       
                        fgets(temp_str,1000,fp);
                }

                for (i=0;i                 {   
                        fscanf(fp,"%f%f",&oldfile.string[i][0],&oldfile.string[i][1]);
                }
               
        }
void xvg_out_file(FILE *fp)
{   
        for (int i=0;i     //fprintf(fp,"%8.5f %10.5f%15.8f\n",newfile.string[i][0],newfile.string[i][1],newfile.string[i][2]);
        fprintf(fp,"%15.5f%15.8f\n",newfile.string[i][0],newfile.string[i][2]);
}
void computer(FILE *fp){
             int i=0,j=0;
                float sum=0;
                 newfile=oldfile;
                for(;i                 {
                        sum=sum+newfile.string[i][1];
       }
                for(i=0;i                 {
                        newfile.string[i][2]=newfile.string[i][1]/sum;
                }


}
int main(int argc, char* argv[])
{  // read the gro
          char newfilename[100];
          char *filename;
          filename=newfilename;
          cout<<"input the input file name:\n";
          cin>>filename;
           FILE *fp,*fp_out;
                    fp=fopen(filename,"r");
                        totalline(fp);
                        rewind(fp);
                    headline(fp);
                        rewind(fp);
                        readxvg(fp);
                        fclose(fp);
                        //total_line+=0;
                        //head_line+=0;

                        computer(fp);
                        //newfile=oldfile;

                       
  //write the gro
         //  cout<<"input the output file name:\n";
                //cin>>filename;
                //fp_out=fopen(filename,"w");
                        fp_out=fopen("avg.xvg","a+");
                xvg_out_file(fp_out);
                fclose(fp_out);       

    return 0;
}

所读文件[ Last edited by dubo on 2011-6-29 at 23:23 ]
2楼2011-06-29 23:20:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huycwork

金虫 (著名写手)

★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
jjdg(金币+1): 感谢参与 2011-06-30 02:31:25
微尘斑竹管的很宽啊,斑竹兼任责任编辑,嗯,最好把那个VC6也改了就更好了~改成perl6~
漩涡的中心有一块空地,空空的。
3楼2011-06-29 23:34:17
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
余泽成(金币+3, 程序强帖+1): 谢谢参与应助! 2011-06-30 17:56:59
多用内置库,fstream比fscanf简单些,好几个数组没必要,一个二维数组保存真正关心的数据就行了,冒号前的数就是下标+1
每行第一个都是1,没必要用数组保存了
gcc,vc6都调试通过了
CODE:
#include
#include
#include
using namespace std;

int main(int argc, char* argv[])
{
        double data[64][94]; // 数据 64*94
        int TR = 0; // 行数

        string sLine; // 保存读取到的每行字符串
        int i; // 循环下标
        int n; // 每行第一个1,扔掉没用
        int num; // 数字,1-94,做下标要减1
        char c; // 字符,接收冒号:,扔掉
        double fData; // 实际data,保存到数组

        ifstream in("bn_pn_train1.scale"); // 输入文件流

        while (getline(in,sLine))
        {
                istringstream is(sLine); // 从字符串构建输入字符串流

                is >> n; // 行首前导1,扔掉

                printf("%d",n);

                for (i=0; i<94; ++i)
                {
                        is >> num; // 得到序号
                        is >> c; // 得到冒号,扔掉
                        is >> fData; // 得到浮点数
                        data[TR][num-1] = fData; // 保存到数组

                        printf(" %d%c%g",num,c,fData);
                }

                printf("\n");

                TR++;
        }

        in.close(); // 关闭文件输入流

        printf("行数TR = %d\n",TR); //打印行数

        return 0;
}

结果

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
4楼2011-06-30 04:58:33
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复(可上传附件)
信息提示
请填处理意见