fprintf未写入文件 [英] fprintf not writing to file

查看:630
本文介绍了fprintf未写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的fprintf语句不执行任何操作.我在它周围放了printf语句,看它是否达到了(确实如此),但我仍然不知道为什么什么也没发生. 这是我的代码:

For some reason, my fprintf statement does nothing. I put printf statements around it to see if it even gets to it (which it does), but I still can't figure out why nothing happens. Here is my code:

#include <stdio.h>
int main(int argc, char *argv[])
{
  char c[8];
  FILE *fp;
  FILE *fp2;
  int i=0;
  int count,j,temp=0;

  fp = fopen(argv[0],"r");
  fp2 = fopen(argv[1], "w");

  for(i=0; i<50;i++)
    {
        count = fread(c,1,8,fp);
        if(((4<i)&&(i<10))||((14<i)&&(i<20))||((24<i)&&(i<30))||((34<i)&&(i<40))||(44<i)){
        continue;
    }
    else{
        for(j = 0; j<count; j++){
            c[j]=c[j]-'0';
            c[j]=c[j] << (count - (j+1));
            temp = temp | c[j];
      }
        fprintf(fp2, "%d\n", temp);
    }
  }

  fclose(fp);
  fclose(fp2);
  return 0;
}

我已经尝试了多种方法,但无法弄清楚!

I've tried multiple things but can't figure it out!

谢谢!.

推荐答案

问题是您使用了argv.如果要传递两个参数,如

The problem is your use of argv. If you want to pass two arguments, as in

./myprog infile outfile

名称infile将出现在argv[1]中,而不是在argv[0]中. outfile也是如此,它将在argv[2]中:

the name infile will be in argv[1], not in argv[0]. Same goes for outfile, it will be in argv[2]:

fp = fopen(argv[1],"r");
fp2 = fopen(argv[2], "w");

为完整起见,该标准对argv[0]的内容进行了如下规定:

For completeness, the standard specifies the content of argv[0] as follows:

如果argc的值大于零,则argv[0]指向的字符串表示程序名称;如果主机环境中没有程序名称,则argv[0][0]为空字符.

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.

这篇关于fprintf未写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆