繁体   English   中英

无法读取文件并传递给参数

[英]Unable to read a file and pass into arguments

1)我试图打开一个文件,读取混合数据(整数,字符和字符串)并将其存储到参数中。

1.1),因此sample.txt中的总数为13(不包括args [0])

2)需要从终端“ ./myprog.c <sample.txt”读取文件

这是我的代码,不知道我哪里出错了:

sample.txt:
123 213 110 90 1
hello my friend
boo bleh
a b c

myprog.c:

#include <stdio.h>
int main()
{
        int i = 1;
        FILE *fstin=fopen(argv[0], "r"); //open the file
        if (fstin == NULL) {
            puts("Couldn't fopen..."); 
            return -1;
        }
         //Getting all the inputs from file
        while ((fscanf(fstin, "%d", argv[i])) != EOF){
            i++;
        }

        fclose(fstin);
        for (i=0; i<10; i++) {
            printf("%d\n",argv[i]);
        }

        return 0;
}

任何帮助是极大的赞赏!

PS:是否有人可以发布完整的解决方案? 将上传到此帖子,并让所有人对此问题进行评论

PPS:请原谅我的编码水平不高,因为我是初学者,并且对C完全陌生。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int ac, char *av[]){
    int i, argc=0;
    char **argv=NULL, data[16];
    FILE *fstin = stdin;

    if(ac == 2){
        if(NULL==(fstin = fopen(av[1], "r"))){
            puts("Couldn't fopen...");
            return -1;
        }
    }
    while (1==fscanf(fstin, "%15s", data)){
        argv = realloc(argv, (argc+1)*sizeof(char*));
        argv[argc] = malloc(strlen(data)+1);
        strcpy(argv[argc++], data);
    }
    if(ac == 2)
        fclose(fstin);

    for (i=0; i<argc; ++i) {
        printf("%s\n", argv[i]);
    }
    //deallocate
    return 0;
}

您在将文件转移到其他错误的第二点时出错。 实际上,您需要首先编译并需要使可执行文件。

gcc -o my_prog ./myprog.c -Wall

您需要执行以下程序以从c程序读取文件:

./my_prog ./sample.txt

当您不熟悉C编程时,首先进入与文件操作相关的手册页。

解:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {

   //If command line argument is not inserted then stop operation
   if (2 !=  argc) {
      printf("Invalid number of arguments : %d\n", argc);
      return -1;
   }
   int size = 0, ret = 0;
   char *data = NULL;
   FILE *fp = NULL;

   //Open file in read mode given from command line argument
   if (NULL != (fp = fopen(argv[1], "r")))
   {
      //Find size of file
      fseek(fp, 0L, SEEK_END);
      size = ftell(fp);
      fseek(fp, 0L, SEEK_SET);
      //if file is empty no need to read it.
      if (size > 0)
      {
         //Data pointer which contains file information
         data = (char *) calloc(sizeof(char), size);
         if (NULL != data)
         {
            //Read whole file in one statement
            fread(data, sizeof(char), size, fp);

            printf("File %s is readed successfully\n", argv[1]);
            printf("Data:\n");
            printf("%s\n", data);
            free(data); data = NULL;
         }
         else
         {
            perror("memory allocation failed\n");
            ret = -1;
         }
      }
      else
      {
         printf("File %s is empty\n", argv[1]);
      }
      fclose(fp); fp = NULL;
   }
   else
   {
      perror("File open failed\n");
      ret = -1;
   }
   return ret;
}

现在在您的设置上对其进行测试,如果有任何疑问,请发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM