简体   繁体   中英

Unable to read a file and pass into arguments

1) I'm trying to open a file, read the mix data (ints, chars and strings) and store them into args.

1.1) so in the sample.txt is a total of 13 (excluding args[0])

2) Need to read a file from terminal "./myprog.c < sample.txt"

Heres my code and have no idea where i went wrong:

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

Any help is greatly appreciated!

PS: Would like if anyone could post their complete solution? Will upload unto this post and let everyone have a review of this problem

PPS: Please excuse the poor level of coding as I am a beginner and completely new to 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;
}

You are making mistake at 2nd point where you divert your file to other file which is wrong. Actually you need to first compile and need to make executable.

gcc -o my_prog ./myprog.c -Wall

You need to execute this program as below to read file from c program:

./my_prog ./sample.txt

As you are new to C programming first go to man pages related to file operations.

Solution:

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

Now Test it on your setup and if any query please post comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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