gpt4 book ai didi

c - fwrite() 动态数组到文件

转载 作者:太空宇宙 更新时间:2023-11-04 00:51:12 25 4
gpt4 key购买 nike

我正在尝试将动态数组保存到文件中,但遇到了一些问题。这是我当前的代码:

/*********************************************************************
*
* ANSI C Example program:
* Acq-IntClk.c
*
* Example Category:
* AI
*
* Description:
* This example demonstrates how to acquire a finite amount of data
* using the DAQ device's internal clock.
*
* Instructions for Running:
* 1. Select the physical channel to correspond to where your
* signal is input on the DAQ device.
* 2. Enter the minimum and maximum voltages.
* Note: For better accuracy try to match the input range to the
* expected voltage level of the measured signal.
* 3. Select the number of samples to acquire.
* 4. Set the rate of the acquisition.
* Note: The rate should be AT LEAST twice as fast as the maximum
* frequency component of the signal being acquired.
*
* Steps:
* 1. Create a task.
* 2. Create an analog input voltage channel.
* 3. Set the rate for the sample clock. Additionally, define the
* sample mode to be finite and set the number of samples to be
* acquired per channel.
* 4. Call the Start function to start the acquisition.
* 5. Read all of the waveform data.
* 6. Call the Clear Task function to clear the task.
* 7. Display an error if any.
*
* I/O Connections Overview:
* Make sure your signal input terminal matches the Physical
* Channel I/O Control. For further connection information, refer
* to your hardware reference manual.
*
*********************************************************************/

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


#define DAQmxErrChk(functionCall) if( DAQmxFailed(error=(functionCall)) ) goto Error; else
FILE *save_file = NULL;

int main(void)
{
int32 error=0;
int32 amount;
int32 counter[2]={'0'};
TaskHandle taskHandle=0;
int32 read;
float64* i_array;
int j;
char errBuff[2048]={'\0'};
char c = 64;
char filename[256];

char answer[2];
const int ia=0;
const char a = 0;

i_array = (float64*)malloc(j*sizeof(int));

/*********************************************/
// DAQmx Configure Code
/*********************************************/

printf("Please enter the amount of voltage checks you wish to run.\n");

while(scanf("%d%c", &amount, &c) !=2) // This is where the user inputs his data. If it isn't a number, it returns false and goes into the while loop.
{
getchar();
puts("Please enter a number.");
}
for (j = 0; j < amount; j++) // Loops through the specified amount
{
DAQmxErrChk (DAQmxCreateTask("",&taskHandle));
DAQmxErrChk (DAQmxCreateAIVoltageChan(taskHandle,"Dev1/ai0","",DAQmx_Val_Cfg_Default,1.0,10.0,DAQmx_Val_Volts,NULL));
DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000));

/*********************************************/
// DAQmx Start Code
/*********************************************/
DAQmxErrChk (DAQmxStartTask(taskHandle));

/*********************************************/
// DAQmx Read Code
/*********************************************/

DAQmxErrChk (DAQmxReadAnalogF64(taskHandle,1000,10.0,DAQmx_Val_GroupByChannel,i_array,1000,&read,NULL));

printf("Updating... ");
printf("Data point %d has %f",j, i_array[j]); // Displays the voltage set number of times
printf(" volts\n");
Sleep(200); // ms pause between each reading

if( taskHandle!=0 )
{
/*********************************************/
// DAQmx Stop Code
/*********************************************/
DAQmxStopTask(taskHandle);
DAQmxClearTask(taskHandle);
}
}

if(j==amount)
{
printf("\nVoltage readings aquired. Please specify a filename so the data can be saved.\n");
goto Save;
}


Save:
printf("\nPlease enter a filename...\n");
gets(filename);
printf("Name is: ");
puts(filename);
save_file = fopen(filename,"w");
printf("File made");

for (j = 0; j < amount; j++)
{
fwrite(&i_array[j], sizeof(j), j, save_file);
printf("Saving...\nSaved.");
}

getchar();
free((void*) i_array);
Error:
if( DAQmxFailed(error) )
DAQmxGetExtendedErrorInfo(errBuff,2048);

if( DAQmxFailed(error) )
printf("DAQmx Error: %s\n",errBuff);

return 0;
}

我会解释我的程序。它使用 NI USB 6211 测量输入和输出,然后显示出来。该程序用于测量进入 ADC 的电压量。它读起来很好,我的动态数组也能正常工作。现在,当涉及到实际写入文件时:

for (j = 0; j < amount; j++)
{
fwrite(&i_array[j], sizeof(j), j, save_file);
printf("Saving...\nSaved.");
}

我收到错误消息“为所需参数传递了空指针”。现在,我知道这是来 self 的 fwrite 代码块,但我已经尝试了很多不同的代码编写方式,但它就是不想遵守。我试过使用指针,但也没有,它只是不会写入数据。 (当我说数据时,我指的是电压读数)。

最佳答案

这个:

i_array = (float64*)malloc(j*sizeof(int));

非常大声地尖叫“损坏的代码”。注意类型不匹配,假设 float64int 大小相同听起来非常危险。

应该是:

i_array = malloc(j * sizeof *i_array);
  1. Don't cast the return value of malloc() in C .
  2. 不要重复类型,在目标指针上使用 sizeof 将类型“锁定”在一起。

此外,您应该始终检查 malloc() 是否成功,如果 j 具有无意义的值,它将失败。

关于c - fwrite() 动态数组到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com