gpt4 book ai didi

c - 将指针传递给文件时出错

转载 作者:行者123 更新时间:2023-11-30 17:06:22 25 4
gpt4 key购买 nike

我还在学习 C,所以请耐心等待。我在将文件指针从函数传递回 main() 时遇到异常错误。

代码如下:

#include "stdafx.h"

#include "FilMst5.c"

int main()
{
FilMstFilPtr = Opn("\\temp\\test.file", "wb"); // <- error occurs here
printf("filptr=0x%p\n", FilMstFilPtr);
return 0;
}

//******************************************************************************
// Open a file
//******************************************************************************
FILE * Opn(char PthNam[], char OpnMod[])
{
FILE * FilPtr = NULL;

errno = fopen_s(&FilPtr, PthNam, OpnMod);
if (errno != 0) {
printf("%s\n", PthNam);
perror("Could not open file");
return NULL;
}
printf("file opened for mode %s\n", OpnMod);
return FilPtr;
}

FilMst5.c 包含文件是:

#pragma once

typedef struct {
int FilRRN;
unsigned long long FilOfs;
unsigned long long FilID;
char FilDir[10 + 1];
char FilNam[10 + 1];
char FilNamLcs[10 + 1];
char FilDirLcs[10 + 1];
char FilTypLcs[10 + 1];
char FilAtr[10 + 1];
char UsrID[10 + 1];
char SysID[8 + 1];
char FilSrcDir[10 + 1];
char FilSrcMbr[10 + 1];
} FilMstStc;
FilMstStc FilMst;


typedef struct {
unsigned long long RcdOfs;
} FilMstL1OfsStc;
FilMstL1OfsStc FilMstL1Ofs[250000000];

typedef struct {
unsigned long long RcdOfs;
} FilMstL1Stc;
FilMstL1Stc FilMstL1Key, FilMstL1SchKey;


typedef struct {
unsigned long long RcdOfs;
} FilMstL2Stc;
FilMstL2Stc FilMstL2[250000000];


typedef struct {
unsigned long long FilSID;
} FilMstL3Stc;
FilMstL3Stc FilMstL3Key, FilMstL3SchKey;


typedef struct {
unsigned long long RcdOfs;
} FilMstL4Stc;
FilMstL4Stc FilMstL4[250000000];


FILE * FilMstFilPtr;

FILE * Opn(char PthNam[], char OpnMod[]);

void WrtFilMstRcd(void)
{
}

从函数返回时得到的错误是:

Unhandled exception at 0x000000013FA41971 in CrtFil5.exe: 0xC0000005:
Access violation writing location 0x000000012E0FEA08.

我期望得到的输出是:

file opened for mode wb
filptr=0x00000000######## <- pointer to file

我只得到第一行,所以函数中的 return 语句和实际返回之间存在一些错误。

真正奇怪的是,如果删除 FilMst5.c 包含文件中的任何行,程序就会按预期工作。但我有一个使用例程的较大程序,我无法删除随机行来尝试使其正常工作。

什么可能导致此问题?

我使用的是带有 Update 1 的 Visual Studio Community Edition 2015,以 x64 模式编译。请让我知道任何有帮助的其他信息。

最佳答案

强烈建议编写“可移植”的代码,即使它只在 Windows 环境中使用:

这是通过在文件中添加前缀来完成的:

#define _CRT_SECURE_NO_WARNINGS

然后使用标准 C 库中的函数,而不是 windows.h header /库中的函数

头文件:FilMst5.h 内容应为:

#pragma once

typedef struct {
int FilRRN;
unsigned long long FilOfs;
unsigned long long FilID;
char FilDir[10 + 1];
char FilNam[10 + 1];
char FilNamLcs[10 + 1];
char FilDirLcs[10 + 1];
char FilTypLcs[10 + 1];
char FilAtr[10 + 1];
char UsrID[10 + 1];
char SysID[8 + 1];
char FilSrcDir[10 + 1];
char FilSrcMbr[10 + 1];
} FilMstStc;

typedef struct {
unsigned long long RcdOfs;
} FilMstL1OfsStc;

typedef struct {
unsigned long long RcdOfs;
} FilMstL1Stc;

typedef struct {
unsigned long long RcdOfs;
} FilMstL2Stc;

typedef struct {
unsigned long long FilSID;
} FilMstL3Stc;

typedef struct {
unsigned long long RcdOfs;
} FilMstL4Stc;

FILE * FilMstFilPtr;

FILE * Opn(char PthNam[], char OpnMod[]);

inline void WrtFilMstRcd(void)
{
}

// enable other files to access the variables declared in main.c
extern FilMstStc FilMst;
extern FilMstL1OfsStc FilMstL1Ofs[250000000];
extern FilMstL1Stc FilMstL1Key;
extern FilMstL1Stc FilMstL1SchKey;
extern FilMstL2Stc FilMstL2[250000000];
extern FilMstL3Stc FilMstL3Key;
extern FilMstL3Stc FilMstL3SchKey;
extern FilMstL4Stc FilMstL4[250000000];
extern FILE * FilMstFilPtr;
<小时/>

文件:(我们称之为main.c)

#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"

#include <stdio.h>

#include "FilMst5.h"

FilMstStc FilMst;
FilMstL1OfsStc FilMstL1Ofs[250000000];
FilMstL1Stc FilMstL1Key;
FilMstL1Stc FilMstL1SchKey;
FilMstL2Stc FilMstL2[250000000];
FilMstL3Stc FilMstL3Key;
FilMstL3Stc FilMstL3SchKey;
FilMstL4Stc FilMstL4[250000000];
FILE * FilMstFilPtr;


int main( void )
{
FilMstFilPtr = Opn("\\temp\\test.file", "wb"); // <- error occurs here
printf("filptr=0x%p\n", FilMstFilPtr);
return 0;
}

//******************************************************************************
// Open a file
//******************************************************************************
FILE * Opn(char PthNam[], char OpnMod[])
{
FILE * FilPtr = NULL;

errno = fopen_s(&FilPtr, PthNam, OpnMod);
if (errno != 0) {
printf("%s\n", PthNam);
perror("Could not open file");
return NULL;
}
printf("file opened for mode %s\n", OpnMod);
return FilPtr;
}

关于c - 将指针传递给文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34770370/

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