Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 使用新表单读取和写入文件_C_File_Writing - Fatal编程技术网

C 使用新表单读取和写入文件

C 使用新表单读取和写入文件,c,file,writing,C,File,Writing,我试图从一个文件中读取,我必须使用一种新的形式,我不确定如何使用它。我已经在下面发布了我必须使用的函数的图片,我不知道如何处理这个错误以及如何修复它?救命啊 #include <stdio.h> #include <string.h> #include <stdlib.h> double* read_file(FILE* file, int length); int main(int argc, char* argv[]) { double* ar

我试图从一个文件中读取,我必须使用一种新的形式,我不确定如何使用它。我已经在下面发布了我必须使用的函数的图片,我不知道如何处理这个错误以及如何修复它?救命啊

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

double* read_file(FILE* file, int length);

int main(int argc, char* argv[])
{
    double* array = malloc(10 * sizeof(double));
    int length = atoi(*(argv + 1));
    FILE* file = *(argv + 2);

    if (argc < 4 || argc > 4)
    {
        printf("Insufficient arguments. Check your command line.\n");
        return 0;
    }

    array = read_file(file, length);
    printf("%p", array);
    return 0;
}

double* read_file (FILE* file, int length)
{
    FILE* ptr;
    double* array = malloc(length * sizeof(double));

    int i = 0;

    if ((ptr = fopen(file, "r")) == NULL)
    {
        return 0;
    }
    else
    {
        for (i = 0; i < length; i++)
        {
            fscanf(ptr, "%lf", (array + i));
        }
    }

    fclose(ptr);

    return array;
}
#包括
#包括
#包括
双*读文件(文件*文件,整数长度);
int main(int argc,char*argv[])
{
double*数组=malloc(10*sizeof(double));
int length=atoi(*(argv+1));
文件*FILE=*(argv+2);
如果(argc<4 | | argc>4)
{
printf(“参数不足。请检查命令行。\n”);
返回0;
}
数组=读取文件(文件,长度);
printf(“%p”,数组);
返回0;
}
双*读文件(文件*文件,整数长度)
{
文件*ptr;
double*array=malloc(长度*sizeof(双精度));
int i=0;
if((ptr=fopen(文件,“r”))==NULL)
{
返回0;
}
其他的
{
对于(i=0;i
首先,您尝试将一个字符字符串分配给指向文件指针类型的变量。编译器不会让你这么做的

// not allowed
FILE* file = *(argv + 2);
其次,您正在将指向文件的指针传递到
fopen()
,但是
fopen()
希望它的第一个参数是一个字符字符串,因此这也不起作用

// file is FILE*, not allowed
ptr = fopen(file, "r"));
如果您修复了这两行代码,那么代码应该编译。

像这样修复

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

//double* read_file(FILE* file, int length);
//You do not need to pass a file pointer, you need a file name.
//It opens the file within this function
double *read_file(const char *filename, int length);

int main(int argc, char* argv[]){
    //if (argc < 4 || argc > 4)
    //argc < 4 || argc > 4 same as argc != 4
    //It is thought that it is 3 because only argv[1] and argv[2] are used.
    //It is necessary to check arguments before using them.
    if (argc != 3) {
        printf("Usage : %s number_of_elements file_name\n", argv[0]);
        printf("Insufficient arguments. Check your command line.\n");
        return 0;
    }

    //double* array = malloc(10 * sizeof(double));//It is not necessary as it is secured by read_file. Make memory leak.
    int length = atoi(argv[1]);
    const char *file = argv[2];

    double *array = read_file(file, length);
    if(array != NULL){
        for(int i = 0; i < length; ++i)
            printf("%f\n", array[i]);
        free(array);
    }
    return 0;
}

double* read_file (const char *file, int length){
    FILE *ptr;

    if ((ptr = fopen(file, "r")) == NULL){
        return NULL;
    }
    //It causes a memory leak unless you first confirm that the file can be opened
    double *array = malloc(length * sizeof(double));

    for (int i = 0; i < length; i++){
        if(1 != fscanf(ptr, "%lf", array + i)){
            printf("Failed to read the %ith element.\n", i+1);
            break;
        }
    }
    fclose(ptr);

    return array;
}
#包括
#包括
#包括
//双*读文件(文件*文件,整数长度);
//您不需要传递文件指针,您需要一个文件名。
//它将在此函数中打开文件
双*读文件(常量字符*文件名,整数长度);
int main(int argc,char*argv[]){
//如果(argc<4 | | argc>4)
//argc<4 | | argc>4与argc!=4相同
//它被认为是3,因为只使用了argv[1]和argv[2]。
//在使用参数之前,必须检查参数。
如果(argc!=3){
printf(“用法:%s\u元素数\u文件名,\n”,argv[0]);
printf(“参数不足。请检查命令行。\n”);
返回0;
}
//double*array=malloc(10*sizeof(double));//这不是必需的,因为它是由read_文件保护的。请创建内存泄漏。
int length=atoi(argv[1]);
const char*file=argv[2];
双*数组=读取文件(文件,长度);
if(数组!=NULL){
对于(int i=0;i
请通过文本而不是图像发布代码。本网站不接受仅使用图像的代码问题/答案,原因多种多样-链接过时,没有人可以复制、构建、实验、增强代码以进行回复等。。。请编辑您的问题,将实际代码剪切/粘贴到堆栈溢出问题编辑器窗口中,缩进4个空格,使其显示为格式化代码块。如果需要先使用外部编辑器缩进,也可以,或者在堆栈溢出编辑窗口中使用cmd-K。看到了吗?编辑窗口图标栏中的帮助,以了解如何正确设置问题和答案的格式。@BradyWebb,因为您是新手,建议参加并检查,以了解如何在此处提出一个好问题(也增加了获得答案的机会)