打开文件,并用C输出 [英] Opening a file and outputting in C

查看:95
本文介绍了打开文件,并用C输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是X code和我试图打开被作为命令行参数传递的文件,并输出被作为该文件的命令行参数传递的行数,以控制台C.在X code,我的论点是TEST.RTF和5。我的RTF格式如下:

  1号线试验
2号线试验
3号线试验
4号线试验
5号线试验
6号线试验
7号线试验
8号线试验
9号线试验
10号线试验

我已经跟我在同一文件夹作为我的X code项目文件夹RTF想这一点,并在该可执行文件是Debug文件夹。我的code是:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#定义CORRECT_PARAMETERS 2
INT主(INT ARGC,CHAR *的argv [])
{
 INT X;
 如果(ARGC!= CORRECT_PARAMETERS){
  的printf(数量的参数输入错误。);
 }
 其他{
  FILE * INFP; / *声明一个文件指针* /
  如果((INFP = FOPEN(的argv [0],R)== NULL)){
   fprintf中(标准错误,无法打开文件);
   出口(EXIT_FAILURE);
  }
  其他{
   为(X = 1; X&下; = argv的[1]; X ++){
    而((X =龟etc(INFP))!= EOF){
      的printf(%C,X);
    }
   }
  }
  FCLOSE(INFP);
 }}

我知道我的code可能不正确在命令行输出行输入的数字,但我不能得到的只是打开文件的开始部分工作。得到什么输出是:

 的输入参数的数目错误。

谢谢!


解决方案

  

在X code,我的论点是TEST.RTF
  和5。


好吧,那么ARGC将采取3的值。


  

的argv [0]:程序的名称


  
  

ARGV [1]:TEST.RTF


  
  

的argv [2]:5


您应该更新你定义的常量取值3。<​​/ P>

  IF((INFP = FOPEN(的argv [0],R)== NULL))

的argv [0]是被执行的程序的名称。

什么你正在寻找(第一个参数)是的argv [1]

  INT X;
为(X = 1; X&下; = argv的[1]; X ++){

这闻起来像麻烦。你是一个比较C字符串为整数。
试试这个(包括使用参数2,而不是1,如上所述):

  INT X;
INT上限=的atoi(argv的[2]);
为(X = 1; X&下; =限制; X ++)

在这里,你正在改变X的值。

 ,而((X =龟etc(INFP))!= EOF)

赋值x = 1只发生一次!!!阅读INFP到另一个变量。

I'm using XCode and I'm trying to open a file that gets passed as a command line argument, and output the number of lines that gets passed as a command line argument of that file, to the console in C. In XCode, my arguments are "test.rtf", and "5". My rtf looks like:

line 1 test
line 2 test
line 3 test
line 4 test
line 5 test
line 6 test
line 7 test
line 8 test
line 9 test
line 10 test

I have tried this with my rtf in the same folder as my XCode project folder, and in the Debug folder where the executable is. My code is:

#include <stdio.h>
#include <stdlib.h>
#define CORRECT_PARAMETERS 2
int main(int argc, char *argv[])
{
 int x;
 if (argc != CORRECT_PARAMETERS) {
  printf("Wrong number of parameters inputted.");
 }
 else {
  FILE *inFp;             /*declare a file pointer */
  if ((inFp = fopen(argv[0], "r") == NULL)) {
   fprintf(stderr, "Can't open file");
   exit(EXIT_FAILURE);
  }
  else {
   for (x = 1; x <= argv[1]; x++) {
    while ((x = fgetc(inFp)) != EOF) {
      printf("%c", x);
    }
   }
  }
  fclose(inFp);
 }

}

I know my code may not be correct of outputting the number of lines input at the command line, but I cannot get the beginning part working of just opening the file. What gets outputted is:

Wrong number of parameters inputted.

Thanks!

解决方案

In XCode, my arguments are "test.rtf", and "5".

Well, then argc will take the value of 3.

argv[0] : name of the program

argv[1]: "test.rtf"

argv[2]: 5

You should update your defined constant to take the value of 3.

 if ((inFp = fopen(argv[0], "r") == NULL))

argv[0] is the name of the program being executed.

What you are looking for (first argument) is argv[1]

int x;
for (x = 1; x <= argv[1]; x++) {

This smells like trouble. You are comparing a c-string to an integer. Try this (including using argument 2 rather than 1, as mentioned above):

int x;
int limit = atoi(argv[2]);
for (x = 1; x <= limit; x++)

Here you are changing the value of X.

 while ((x = fgetc(inFp)) != EOF)

The assignment x=1 only happens once !!!. Read inFp into another variable.

这篇关于打开文件,并用C输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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