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

标签 c windows visual-studio-2015

我还在学习 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/

相关文章:

visual-studio-2015 - 错误 MSB3073 : The command exited with code 3

c++ - 如何获取一个简单的批处理文件以在 Visual Studio 中与我的项目一起运行?

windows - 从 Visual Studio 运行时,STARTUPINFO.wShowWindow 为 0

c - 在运行时收到 "cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 220 (OpenCL 2.2)"警告

visual-studio-2015 - TypeScript 编译正常,但仍然出现智能感知错误

c - 以上是有效的 C 代码吗?如果是这样,它试图达到什么目的,为什么有人会做上面那样的事情?

mysql - MySQL8.0无法重置root密码: windows,

c - 如何在多线程矩阵乘法中使用线程池和消息队列?

c - 2个while循环内的变量不会重置

c - 有没有办法检测文件依赖项何时满足 "accidentally"?