gpt4 book ai didi

c - 在 Visual Studio C 中写入文件

转载 作者:行者123 更新时间:2023-11-30 15:14:11 28 4
gpt4 key购买 nike

因此,在尝试了我实现的解决方案后,我得到的输出为“退出”。

我正在尝试写入某个文件,最好是 csv 文件。我写入的任何文件都会抛出错误消息,附加。我不知道出了什么问题。

这是我在 Visual Studio 2015 C 中的代码

#define TRANSFER_LENGTH_FROM_HOST (128)                 //Burst input
#define TRANSFER_LENGTH_FROM_FPGA (128) // Burst output
#define NUM_TRANSFERS (50)

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ZestSC2.h"

void ErrorHandler(const char *Function, ZESTSC2_HANDLE Handle, ZESTSC2_STATUS Status, const char *Msg)
{
printf("**** Example2 - Function %s returned an error\n \"%s\"\n\n", Function, Msg);
exit(1);
}

int main(int argc, char **argv)
{
unsigned long Count;
unsigned long NumCards;
unsigned long CardIDs[256];
unsigned long SerialNumbers[256];
ZESTSC2_FPGA_TYPE FPGATypes[256];
ZESTSC2_HANDLE Handle;
unsigned __int64 Freq;
unsigned __int64 Start;
unsigned __int64 End;
unsigned long *Buffer_tx;
unsigned long *Buffer_rx;

FILE *fp;
errno_t err = fopen_s(&fp, "C:/Users/Shankhadeep/Documents/results.csv", "w+");

// Install an error handler
ZestSC2RegisterErrorHandler(ErrorHandler);

// Request information about the system
ZestSC2CountCards(&NumCards, CardIDs, SerialNumbers, FPGATypes);

printf("%d available cards in the system\n\n\n", NumCards);

if (NumCards==0)
{
printf("No cards in the system\n");
exit(1);
}

for (Count=0; Count<NumCards; Count++)
printf("%d : CardID = 0x%08lx, SerialNum = 0x%08lx, FPGAType = %d \n \n", Count, CardIDs[Count], SerialNumbers[Count], FPGATypes[Count]);

// Open the first card
ZestSC2OpenCard(CardIDs[0], &Handle);

printf("Reading the bit file .. \n");

if (FPGATypes[0] == ZESTSC2_XC3S2000)
ZestSC2ConfigureFromFile(Handle, "C:/Users/Shankhadeep/Documents/Example2_4000.bit");
else
ZestSC2ConfigureFromFile(Handle, "C:/Users/Shankhadeep/Documents/example2.bit");

// Send data to the card - Generate a 64-bit random number in Buffer
Buffer_tx = malloc(TRANSFER_LENGTH_FROM_HOST);
Buffer_rx = malloc(TRANSFER_LENGTH_FROM_FPGA);

for (Count = 1; Count < TRANSFER_LENGTH_FROM_HOST / 2; Count++) {
((unsigned long *)Buffer_tx)[Count] = (unsigned long)(1 << (Count - 1));
printf("Writing %x \n", Buffer_tx[Count]);
}

QueryPerformanceFrequency((LARGE_INTEGER *)&Freq);
QueryPerformanceCounter((LARGE_INTEGER *)&Start);
for (Count=0; Count<NUM_TRANSFERS; Count++)
{
ZestSC2WriteData(Handle, Buffer_tx, TRANSFER_LENGTH_FROM_HOST);
for (Count = 0;Count<TRANSFER_LENGTH_FROM_HOST / 2; Count++)
//fwrite(Buffer_tx, 64, 128, fp);
//Throws an error as soon as I write this line
fprintf(fp, "%s", Buffer_tx[Count]);
}
QueryPerformanceCounter((LARGE_INTEGER *)&End);
printf("Transfer rate to card = %.2fMBytes/sec\n", ((double)NUM_TRANSFERS*(double)TRANSFER_LENGTH_FROM_HOST*(double)Freq)/(1024.*1024.*(double)(End-Start)));

// Read data from the card
QueryPerformanceCounter((LARGE_INTEGER *)&Start);
// for (Count=0; Count<NUM_TRANSFERS; Count++)
// {
ZestSC2ReadData(Handle, Buffer_rx, TRANSFER_LENGTH_FROM_FPGA);
for (Count = 0; Count < 64; Count++) {
printf("Reading %x \n", Buffer_rx[Count]);

}
// }
QueryPerformanceCounter((LARGE_INTEGER *)&End);
printf("\n");
printf("Transfer rate from card = %.2fMBytes/sec\n",
((double)NUM_TRANSFERS*(double)TRANSFER_LENGTH_FROM_FPGA*(double)Freq)/(1024.*1024.*(double)(End-Start)));
printf("End : %d Start: %d \n", End, Start);

if (!fp)perror("fopen_s");
printf("Error is %d", err);

if (err != NULL)
fprintf(fp, "Hello");
else printf("Exit \n");
ZestSC2CloseCard(Handle);
return 0;
}

我收到的错误是

Debug Assertion Failed!
Program .. fpga_2_host.exe
File minkernel\crts\ucrt\src\appcrt\stdio\output.cpp Line 31
Expression: stream != nullptr

最佳答案

您错误地使用了 fopen_s 的返回值 ( It returns an error code )。第一个参数是将数据设置到 fp 中所需的全部内容。

errno_t err = fopen_s(&fp, "C:/Users/Shankhadeep/Documents/results.csv", "w+");

关于c - 在 Visual Studio C 中写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34141659/

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