简体   繁体   English

命令行参数问题

[英]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. 目前程序正在读取“无法打开输入文件”,这意味着大小为0。我用编辑器制作了输入文件,但不确定是什么问题。 Is there anything up with my code that could cause this? 我的代码有什么问题可能导致这种情况? Or is it more likely I just messed up the input.txt file? 还是更有可能只是搞砸了input.txt文件?

#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; 您是逗号行中的文件名,但没有将其存储在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. int load_data(char* filename, int *x, float *y)您传递的是filename但是filename中没有存储name of file
  2. fscanf(file, "%d%f", &x, &y); when you pass pointer in fscanf with %d you don't need & operator.Just this will do- 当您使用%dfscanf传递指针时,您不需要&运算符。

     fscanf(file, "%d%f", x, y); 
  3. You need to allocate memory using malloc to x and y . 您需要使用malloc为xy分配内存。

  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 . 这两个函数的size不同,因为您在函数和main再次声明了它。这就是为什么int main size始终为0原因。

  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. void print_data在此函数中,您正在打印acnamt值,但是两个指针都未初始化,并且正在打印它,因此它将给出未定义的行为。

  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 . 在下面的代码行(已发布)中, size变量的值为0 The value has never been updated, before checking at line if(size==0) . 在检查if(size==0)行之前,从未更新过该值。 That is why this if check is returning true and printing "Unable to open the input file" . 这就是为什么if check返回true并显示"Unable to open the input file"

You may want to set the value of size variable before this if check. if检查,您可能需要在此之前设置size变量的值。

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

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

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