如何用一个文件和一个malloc构造一个函数 [英] How to structure a function with a file and a malloc

查看:140
本文介绍了如何用一个文件和一个malloc构造一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $ $ $ $ $ $ $ $ $> int funzione(int *)如果我有一个函数可以处理文件和指针,兽医)
{
FILE * fin;

if(!(fin = fopen(name,r)))
return 1;
$ b $ *从文件中读取信息(如num)* /

vet =(int *)malloc(num * sizeof(int));

/ * ... * /

return fclose(fin);

$ / code>

如何返回 vet
谢谢。

解决方案

如果您想通过返回值本身以外的方式返回,您可以简单地通过在一个 double 指针和取消引用。



严格按值传递(给函数的所有东西都是一个 原来的改变并不会反映给调用者),但是你可以用指针来模拟pass-by-reference。你会看到像这样:

$ p $ int funzione(int ** pVet)
{
FILE *鳍;

if(!(fin = fopen(name,r)))
return 1;
$ b $ *从文件中读取信息(如num)* /

* pVet = malloc(num * sizeof(int));

/ * ... * /

return fclose(fin);





$ p你可以这样调用它:

  int * myVet; 
int result = funzione(&myVet);

你会注意到我也从 malloc - 在C中这样做是一个坏主意,因为它可以隐藏某些细微的错误。



你不应该犯这个错误,因为你传入了一个 int 指针,它会反射回来。如果是你想改变的 int ,那就没问题了。但是,因为它是你想改变的 int 指针,所以你需要一个指向的指针。






但是请记住,返回NULL的常见方法是返回NULL。这是否适合你的情况是你必须决定的。如果可能的话,我会考虑返回指针本身,并使用NULL作为数据无法获得的指示(无法打开文件,无法分配内存,数据错误,无法关闭文件等)

那会是这样:

  int * funzione void){
FILE * fin = fopen(name,r);
if(fin == NULL)return NULL;
$ b $ *从文件中读取信息(如num)* /

int * pVet = malloc(num * sizeof(int));
if(pVet == NULL)返回NULL;

//根据需要填充pVet。任何错误必须在
//返回NULL之前释放它,例如:
$ b $ if(fclose(fin)!= 0){
free(pVet);
返回NULL;
}

return pVet;
}


If a I have a function that works with a file and a pointer, like this:

int funzione (int *vet)
{
    FILE *fin;

    if ( !(fin = fopen(name, "r")) )
        return 1;

    /* read informations from file (like num) */

    vet = (int *) malloc (num*sizeof(int));

    /* ... */

    return fclose(fin); 
}

How can I return vet? Thank you.

解决方案

If you want to return it via other than the return value itself, you can simply pass in a double pointer and dereference that.

C is strictly pass-by-value (everything given to a function is a copy of the original and changes to it won't be reflected back to the caller) but you can emulate pass-by-reference with pointers. You would be looking at something like:

int funzione (int **pVet)
{
    FILE *fin;

    if ( !(fin = fopen(name, "r")) )
        return 1;

    /* read informations from file (like num) */

    *pVet = malloc (num * sizeof(int));

    /* ... */

    return fclose(fin); 
}

and you would call it thus:

int *myVet;
int result = funzione (&myVet);

You'll notice I've also removed the cast on return from malloc - it's a bad idea to do this in C since it can hide certain subtle errors.

You shouldn't make the mistake of thinking, because you're passing in an int pointer, that it will reflect back. If it was the int you wanted to change, that would be okay. But, since it's the int pointer you want to change, you need a pointer to that.


However, keep in mind that a common method of returning an error where pointers are involved is simply to return NULL. Whether that's suitable in your case is something you have to decide. If possible, I would consider returning the pointer itself and using NULL as an indication that the data wasn't obtainable (couldn't open file, couldn't allocate memory, data was wrong somehow, couldn't close file and so on).

That would go something like:

int *funzione (void) {
    FILE *fin = fopen(name, "r");
    if (fin == NULL) return NULL;

    /* read informations from file (like num) */

    int *pVet = malloc (num * sizeof(int));
    if (pVet == NULL) return NULL;

    // Populate pVet as needed. Any error must free it before
    //   returning NULL, such as with:

    if (fclose(fin) != 0) {
        free (pVet);
        return NULL;
    }

    return pVet;
}

这篇关于如何用一个文件和一个malloc构造一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆