24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 2546  |  回复: 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的回帖

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的回帖

dp.xiong

新虫 (初入文坛)


dubo(金币+1): 欢迎常来程序语言版讨论 2011-06-30 18:44:52
引用回帖:
Originally posted by dp.xiong at 2011-06-29 22:14:30:
[code]#include <stdio.h>
#include <stdlib.h>

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

int main()
{
     int TR=0,i; //TR表示文件数 ...

非常感谢,我得好好看看了,呵呵
5楼2011-06-30 15:14:36
已阅   回复此楼   关注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的回帖

dp.xiong

新虫 (初入文坛)


dubo(金币+1): 欢迎常来程序语言版讨论 2011-06-30 18:44:34
引用回帖:
Originally posted by libralibra at 2011-06-30 04:58:33:
多用内置库,fstream比fscanf简单些,好几个数组没必要,一个二维数组保存真正关心的数据就行了,冒号前的数就是下标+1
每行第一个都是1,没必要用数组保存了
gcc,vc6都调试通过了
[code]
#include <fstream> ...

厉害,果然高手中的高手,谢谢你的帮助,请教了
7楼2011-06-30 15:18:14
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 dp.xiong 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 材料求调剂 +11 一样YWY 2026-04-05 11/550 2026-04-05 23:36 by 来看流星雨10
[考研] 308求调剂 +3 终不似从前 2026-04-05 3/150 2026-04-05 20:07 by 啵啵啵0119
[考研] 304求调剂 +3 luoye0105 2026-04-05 3/150 2026-04-05 18:16 by 土木硕士招生
[考研] 一志愿南航,数一英一学硕317求调剂!! +5 Acaciad 2026-04-04 5/250 2026-04-05 12:31 by 搏击518
[考研] 085410人工智能 初试316分 求调剂 +6 残星拂曙 2026-03-31 6/300 2026-04-05 12:15 by rainbow11
[考研] 341求调剂 +3 学无止境,冲 2026-04-05 3/150 2026-04-05 09:40 by lbsjt
[考研] 材料与化工306分找调剂 +12 沧海轻舟e 2026-04-03 13/650 2026-04-04 23:45 by lqwchd
[考研] 专硕310求调剂 +5 捞捞我…. 2026-04-04 6/300 2026-04-04 23:33 by barlinike
[考研] 331求调剂 +3 niby 2026-04-02 3/150 2026-04-04 19:56 by 蓝云思雨
[考研] 土木304求调剂 +4 兔突突突, 2026-03-31 4/200 2026-04-04 13:34 by 1753564080
[考研] 335求调剂 +7 沈清璃 2026-04-03 7/350 2026-04-03 18:55 by lijunpoly
[考研] 考研调剂 +8 不爱喝饮料 2026-04-03 8/400 2026-04-03 16:40 by Mistake-J
[考研] 326分求调剂 +3 于是乎呢 2026-04-01 5/250 2026-04-03 14:23 by 于是乎呢
[考研] 学硕化学工程与技术,一志愿中国海洋大学320+求调剂 +8 披星河 2026-04-02 8/400 2026-04-02 14:12 by oooqiao
[考研] 能源动力 调剂 +3 不破不立0 2026-04-02 3/150 2026-04-02 12:46 by ffffjjjj
[考研] 07生物学求调剂 一志愿同济大学359分 +3 LAMC. 2026-03-30 3/150 2026-04-02 10:26 by 18828373951
[考研] 省双一流重点一本大学招收调剂 +4 wwwwffffff 2026-03-31 7/350 2026-04-01 15:23 by wwwwffffff
[考研] 301求调剂 +8 axibli 2026-04-01 8/400 2026-04-01 09:51 by 我的船我的海
[考研] 一志愿中海洋材料357 +4 麦恩莉. 2026-03-30 4/200 2026-03-31 14:35 by 记事本2026
[考研] 一志愿西电085401数一英一299求调剂 六级521 +4 爱吃大鸭梨 2026-03-31 4/200 2026-03-31 11:51 by 搏击518
信息提示
请填处理意见