简体   繁体   中英

command line arguments issues

At the moment the program is reading "unable to open the input file" which means the size is 0. I made the input file with my editor, but I'm not sure what the issue could be. Is there anything up with my code that could cause this? Or is it more likely I just messed up the input.txt file?

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

int load_data(char* filename, int *x, float *y)
{
    int i=0;

    FILE* file=fopen(filename,"r");


    if(file==NULL)
    {
            return 0;
    }

    int size;


    fscanf(file, "%d", &size);

    for(i=0;i<size;i++)
    {
            fscanf(file, "%d%f", &x, &y);
    }


    fclose(file);
    return size;
}


void print_data(int *acn, float *amt, int size)
{
    int i;
    int *p;

    for(i=0;i<size;i++)
    {
            printf("%-10d%-10f ", *(acn+i), *(amt+i));
    }
}

int main(int argc, char** argv)
{
    int size=0, *x;
    char *filename;
    float *y;

    if(argc!=3)
    {
            printf("\nInsufficient arguments.\n");
            return 0;
    }


    int n=atoi(argv[2]);

    int *acn;
    float *amt;


    int *fp=malloc(sizeof(int)*n);


    if(size==0)
    {
            printf("\nUnable to open the input file.\n");
            return 0;
    }
    load_data(filename, x, y);
    print_data(acn, amt, size);

    free(fp);
    return 0;
}

There are number of problems in you program -

  1. You are name of file from commal line but you do not store it in char *filename; and in function int load_data(char* filename, int *x, float *y) you are passing filename .But filename does not have the name of file in it stored.
  2. fscanf(file, "%d%f", &x, &y); when you pass pointer in fscanf with %d you don't need & operator.Just this will do-

     fscanf(file, "%d%f", x, y); 
  3. You need to allocate memory using malloc to x and y .

  4. size in both functions are different as you declare it again in the function and in main .Thats why size is always 0 in int main .

  5. void print_data in this function you are printing value of acn and amt but both the pointer are unintialized and you are printing it so it will give undefined behaviour.

  6. Also you have pointers which are declared in your program but not used .

In following lines of code (which you have posted), the value of size variable is 0 . The value has never been updated, before checking at line if(size==0) . That is why this if check is returning true and printing "Unable to open the input file" .

You may want to set the value of size variable before this if check.

int size=0, *x;    //HERE YOU ARE WRITING "SIZE" VARIABLE
char *filename;
float *y;

if(argc!=3)
{
        printf("\nInsufficient arguments.\n");
        return 0;
}

int n=atoi(argv[2]);
int *acn;
float *amt;

int *fp=malloc(sizeof(int)*n);
if(size==0) //HERE YOU ARE READING/CHECKING "SIZE" VARIABLE. THERE IS NO CHECGE IN VARIABLE BEFORE THIS SO, VALUE IS STILL '0'
{
        printf("\nUnable to open the input file.\n");
        return 0;
}

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