无法读取文件并传入参数

时间:2014-04-17 12:37:22

标签: c linux

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

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来说是全新的。

2 个答案:

答案 0 :(得分:-1)

#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;
}

答案 1 :(得分:-1)

你在第2点错误地将文件转移到错误的其他文件。实际上你需要先编译并需要生成可执行文件。

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;
}

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