Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中加载长度为0的数组_C_Arrays_Function_Save_Load - Fatal编程技术网

在C中加载长度为0的数组

在C中加载长度为0的数组,c,arrays,function,save,load,C,Arrays,Function,Save,Load,每当加载函数遇到数组长度为0的情况时,我都会收到此错误。我将非常感谢任何帮助解决这个问题!收到的代码和错误分别为: /* Save the entire array ia into a file called 'filename' in a JSON text file array file format that can be loaded by intarr_load_json(). Returns zero on success, or a non-zero error code on f

每当加载函数遇到数组长度为0的情况时,我都会收到此错误。我将非常感谢任何帮助解决这个问题!收到的代码和错误分别为:

/*
Save the entire array ia into a file called 'filename' in a JSON
text file array file format that can be loaded by
intarr_load_json(). Returns zero on success, or a non-zero error
code on failure. Arrays of length 0 should produce an output file
containing an empty array.

The JSON output should be human-readable.

Examples:

The following line is a valid JSON array:
 [ 100, 200, 300 ]

The following lines are a valid JSON array:
[
100,
200,
300
]
*/ 

int intarr_save_json( intarr_t* ia, const char* filename )
{
    if (ia == NULL)  {
        return 1;
    }
    int *arr=ia->data;
    if (arr == NULL) { 
        return 1;
    }
    int n=ia->len;
    if (n == 0 ) {
        return 0;
    }
    int i;
    FILE *p;
    p=fopen(filename,"w");

    if(p!=NULL)
    {
        fprintf(p,"[\n");
        for(i=0;i<n;i++)
        {
            if(i!=n-1)
            {
                fprintf(p," %d,\n",arr[i]);
            }
            else
            {
                fprintf(p," %d\n",arr[i]);
            }
        }
        fprintf(p,"]");
        fclose(p);
        return 0;
    }
    return 1;
}

/*
Load a new array from the file called 'filename', that was
previously saved using intarr_save(). The file may contain an array
of length 0. Returns a pointer to a newly-allocated intarr_t on
success (even if that array has length 0), or NULL on failure.
*/

intarr_t* intarr_load_json( const char* filename ){ 
    if (filename == NULL) { 
        return NULL;
    }
    FILE* f = fopen(filename, "r");
    if (f == NULL) { 
        return NULL;
    }

    intarr_t* loaded = intarr_create(0);

    char c;
    fscanf(f, "%c ", &c);

    int value;
    while (fscanf(f, "%d, ", &value)) {
        intarr_push(loaded, value);
    }
    if (fscanf(f, "%d ", &value)){
        intarr_push(loaded, value);
    }

    fclose(f);
    return loaded;
}
/*
将整个数组保存到JSON格式的名为“filename”的文件中
可由加载的文本文件数组文件格式
intarr\u load\u json()。成功时返回零,或返回非零错误
故障代码。长度为0的数组应生成输出文件
包含空数组的。
JSON输出应该是可读的。
示例:
以下行是有效的JSON数组:
[ 100, 200, 300 ]
以下行是有效的JSON数组:
[
100,
200,
300
]
*/ 
int intarr\u save\u json(intarr\u t*ia,const char*filename)
{
如果(ia==NULL){
返回1;
}
int*arr=ia->data;
如果(arr==NULL){
返回1;
}
int n=ia->len;
如果(n==0){
返回0;
}
int i;
文件*p;
p=fopen(文件名,“w”);
如果(p!=NULL)
{
fprintf(p,“[\n”);

对于(i=0;iSorry,但我看不到任何函数调用…该学习了。欢迎使用堆栈溢出!您介意创建一个吗? Saving array len 0: intarr_save_json( 0x1277010, "saved_array.0" ) Loading array intarr_load_json( "saved_array.0" ) Error: intarr_load_json() returned NULL pointer.