简体   繁体   English

在C中的fprintf上以不同的名称保存多个文件

[英]Save multiple files with different names on fprintf in C

I want to save multiple .dat files but apears the warning: "format '%f' expects argument of type 'double', but argument 3 has type 'double *' [-Wformat=]" and the files come out empty. 我想保存多个.dat文件,但发出警告:“格式'%f'期望类型为'double'的参数,但参数3的类型为'double *'[-Wformat =]”,文件空着。

FILE *f1;
double hist[N];
double delta_t = 0.25;
int n_periodos = 0;
char name[100]; 
sprintf(name,"testeT%f.dat",n_periodos*delta_t);
f1 = fopen (name,"w");
fprintf(f1,"%lf",hist); //The problem is here

Your problem in the last line is you are passing hist (which is an array of double ) to fprintf where you have used the %lf conversion specifier which expects a double as its argument (not an array of double ) 最后一行的问题是将hist (这是double数组 )传递给fprintf ,在其中使用了%lf转换说明符,该说明符期望double作为其参数(而不是double数组

When you declare an array in C, on access, the array is converted to a pointer to the first element in the array C11 Standard - 6.3.2.1 Lvalues, arrays, and function designators(p3) . 在C中声明数组时,在访问时,该数组将转换为指向数组C11 Standard-6.3.2.1的Lvalues,数组和函数指示符(p3)中第一个元素的指针。 (the exceptions are stated there -- when use with sizeof , _Alignof , or with the unary & operator, or when initialized with a string literal ) None of those apply here. (例外在此处说明-当与sizeof_Alignof或一元&运算符一起使用时,或者在使用字符串常量初始化时)那些都不适用。

So with your declaration of: 因此,您声明:

double hist[N];

hist is an array of double. hist是双hist数组。 When you use hist in: 在以下位置使用hist时:

fprintf (f1, "%lf", hist);

hist is converted to a pointer to the first element in the array (eg the address of the first element) which has a type 'double*' . hist转换为指向数组中第一个元素(例如第一个元素的地址 )的指针,该指针的类型为'double*' To correct the problem, you need to dereference the pointer (generally done with an array by using [element] following the varaible, eg 要解决此问题,您需要取消引用指针(通常通过在变量之后使用[element]对数组进行引用,例如

fprintf (f1, "%lf", hist[0]);  /* or element 1, 2, 3, .... */

This will make your types consistent. 这将使您的类型一致。

You could rewrite your code (thought it is still unclear what N is), to eliminate the issue and correct some other shortcomings (identified in the comments below) 您可以重写代码(以至于仍不清楚N是什么),以消除该问题并纠正其他缺陷(在下面的注释中指出)

#include <stdio.h>

#define MAXC  100   /* if you need a constant, #define on (or more) */
#define NHIST  32   /* it is unclear where N came from in your code */

int main (void) {

    FILE *f1 = NULL;            /* initialize all variables and help */
    double hist[NHIST] = {0.0}; /* avoid Undefined Behavior :)       */
    double delta_t = 0.25;
    int n_periodos = 0;
    char name[MAXC] = ""; 

    /* use snprintf to protect the bounds of 'name' */
    snprintf (name, MAXC - 1, "testeT%f.dat", n_periodos*delta_t);

    f1 = fopen (name, "w");     /* call fopen */
    if (f1 == NULL) {           /* validate file is open for writing */
        perror ("fopen-name");
        return 1;
    }

    fprintf (f1, "%lf", hist[0]);
}

Look things over and let me know if you have further questions. 仔细检查一下,如果您还有其他问题,请告诉我。

When you call runtime functions it is important to check the return values to see if they were successful or not. 调用运行时函数时,重要的是检查返回值以查看它们是否成功。

Another thing to keep in mind is that float and doubles are not exact values so using them as part of a file name is not good. 要记住的另一件事是,float和double值不是精确值,因此将它们用作文件名的一部分不是很好。

So check the return value 所以检查返回值

f1 = fopen(name, "w");
if (f1 != NULL)
{
  ...
  fclose(f1);
}
else
{
  perror(name); // or write out the error
}

Note also that variables declared are not necessary 0 if you declare them in a function, they can have arbitrary values so you need to initialize them 还要注意,如果在函数中声明变量,则声明的变量不是必需的0,它们可以具有任意值,因此需要初始化它们

double hist[N] = {0};

When you write the hist[] to a file you cannot use fprintf like that, you should loop through the values writing them one value at a time, fprintf can't handle writing an array like you wrote. 当您将hist []写入文件时,您不能像这样使用fprintf,您应该循环遍历这些值,一次将它们写入一个值,fprintf无法像您编写的那样处理数组。

for (int i = 0; i < N; ++i)
{
  fprintf(f1, "%lf\n", hist[i]); // added \n as delimiter
}

hist is an array of doubles (or technically a pointer, double* , as its passed into fprintf), but you are attempting to write a single double value to file, not an array. hist是一个double数组(或从技术上讲,是指针double* ,它传递给fprintf),但是您试图将一个double值写入文件,而不是数组。 You probably want something like this to write the entire array: 您可能想要这样写整个数组:

for (int i = 0; i < N; i++)
{
    fprintf(f1, "%f", hist[i]);
}

Or just a single value: 或者只是一个值:

fprintf(f1, "%f", hist[0]);

Also, in your sample code, hist is an uninitialized array. 此外,在示例代码中, hist是未初始化的数组。 What gets written to file will likely not be what you expect. 写入文件的内容可能不是您期望的。

regarding: 关于:

fprintf(f1,"%lf",hist);

in C, a reference to the name of an array 'degrades' to the address of the first byte of the array. 在C语言中,对数组名称的引用“降级”为该数组第一个字节的地址。

Much better to use: 更好地使用:

fprintf(f1,"%lf",hist[0]);

as that will output the contents of the first entry in the array. 因为它将输出数组中第一个条目的内容。

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

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