标题:写了程序出错了,可是不知道错在哪里
只看楼主
jacktdt8387
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-1
结帖率:0
已结贴  问题点数:20 回复次数:1 
写了程序出错了,可是不知道错在哪里
#include <stdio.h>
#include <stdlib.h>


void operation(FILE* ifp,FILE* ofp)
{
    float a, b;
    const int LOOPLIMIT =10000;
   
          if ( (ifp==NULL) || (ofp==NULL) ){
          fprintf(stderr,"[Error] null filr pointer detected.\n");
          exit(-1);
    }
   
    for (int i=0; i<LOOPLIMIT; i++) {
        if (fscanf(ifp,"%f%f",&a,&b) == EOF)
            break;
        float c = a *b;
        fprintf(ofp, "%g, %g, %g\n",a,b,c);
    }
}

int main(int argc, char* argv[])
{
    FILE* ifp = NULL;
    FILE* ofp = NULL;
    char* ifile = NULL;
    char* ofile = NULL;
   
    if(argc!=3) {
        fprintf(stderr,"usage: %s inputfile outputfile\n", argv[0]);
        exit(1);

    }
    ifile =argv[1];
    ofile =argv[2];
   
    if( (ifp = fopen(ifile,"rt"))==NULL) {
        fprintf(stderr,"can not open file %S\a\n", ifile);
        exit(2);
    }
      if( (ifp = fopen(ifile,"wt"))==NULL) {
        fprintf(stderr,"can not open file %S\a\n", ifile);
        exit(3);
    }
   
    operation(ifp,ofp);
   
    fclose(ofp);
    fclose(ifp);
    return(0);
   
}



搜索更多相关主题的帖子: include pointer operation 
2012-10-01 23:26
zhu224039
Rank: 8Rank: 8
等 级:贵宾
威 望:17
帖 子:862
专家分:792
注 册:2012-7-29
得分:20 
#include <stdio.h>
#include <stdlib.h>


void operation(FILE* ifp,FILE* ofp)
{
    float a, b;
    const int LOOPLIMIT =10000;
   
          if ( (ifp==NULL) || (ofp==NULL) ){
          fprintf(stderr,"[Error] null filr pointer detected.\n");
          exit(-1);
    }
   
    for (int i=0; i<LOOPLIMIT; i++) {                   -----------------------》int i 你放到声明部分去
        if (fscanf(ifp,"%f%f",&a,&b) == EOF)
            break;
        float c = a *b;                                  ---------------》float c 也放到声明部分去  C语言的规则是 先声明 后使用函数体内部接受变量声明
        fprintf(ofp, "%g, %g, %g\n",a,b,c);
    }
}

int main(int argc, char* argv[])
{
    FILE* ifp = NULL;
    FILE* ofp = NULL;
    char* ifile = NULL;
    char* ofile = NULL;
   
    if(argc!=3) {
        fprintf(stderr,"usage: %s inputfile outputfile\n", argv[0]);
        exit(1);

    }
    ifile =argv[1];
    ofile =argv[2];
   
    if( (ifp = fopen(ifile,"rt"))==NULL) {
        fprintf(stderr,"can not open file %S\a\n", ifile);
        exit(2);
    }
      if( (ifp = fopen(ifile,"wt"))==NULL) {
        fprintf(stderr,"can not open file %S\a\n", ifile);
        exit(3);
    }
   
    operation(ifp,ofp);
   
    fclose(ofp);
    fclose(ifp);
    return(0);
   
}
你使用的是C语言 不是C++ C++可以在程序段任意地方声明变量,但是C不行 所有变量都必须先在函数开始处声明后再使用
问题 解决了就请快点结贴吧,穷的裤腰带都要掉地上去了

我要成为嘿嘿的黑客,替天行道
2012-10-02 02:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-382429-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.735739 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved