得到错误传递一个指针文件 [英] getting error passing a pointer to file

查看:109
本文介绍了得到错误传递一个指针文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习C,所以请多多包涵。我发现了一个不同寻常的错误传递一个指针,从函数文件回的main()

I'm still learning C, so please bear with me. I'm getting an unusual error passing a pointer to file back from a function to main().

在code如下:

#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包括文件是:

The FilMst5.c include file is:

#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)
{
}

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

The error I get, on returning from the function is:

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

我希望得到的输出是:

The output I expected to get would be:

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

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

I only get the first line, so there's some error between the return statement in the function and the actual return.

什么是真正奇怪的是,如果你删除任何在FilMst5.c行的包含文件,根据需要程序工作。但我有一个使用程序一个更大的计划,我不能随意删除线,试图得到它的工作。

What is really strange is that if you delete any of the lines in the FilMst5.c include file, the program works as desired. But I have a larger program that uses the routines and I can't delete random lines to try to get it working.

这是什么原因这个问题?

What could cause this problem?

我使用Visual Studio社区版与2015年1更新,在64位模式下编译。请让我知道任何额外的信息,将是有益的。

I'm using Visual Studio Community Edition 2015 with Update 1, compiled in x64 mode. Please let me know any additional info that would be helpful.

推荐答案

强烈建议写code是便携,哪怕是只打算在Windows环境下使用:

Strongly suggest writing code that is 'portable', even if it is only going to be used in a windows environment:

这是由prefixing将该文件与完成的:

This is accomplished by prefixing the file with:

#define _CRT_SECURE_NO_WARNINGS

然后使用功能从标准C库,而不是从 WINDOWS.H 头/库

头文件:FilMst5.h内容应该是:

the header file: FilMst5.h contents should be:

#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;
}

这篇关于得到错误传递一个指针文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆