无法使用 c 的 fopen() 打开放入工作目录的现有文件

标签 c gps fopen

我正在开发一个基于开源库的 GPS 处理软件。该库为我提供了一个 RINEX 文件阅读器,它可以打开包含 GPS 导航消息或观测数据以及许多数据处理功能的现有文件。上述引用的 GPS 数据文件的内容用于填充执行数据处理的算法所使用的结构成员。

我的问题是,RINEX 阅读器功能可以正确读取包含观测数据的文件(文件类型:yyO,其中 yy 是观测年份最右边的两位数字),但它无法打开我给出的任何导航消息文件输入(文件类型:yyN)。

经过调试 session 后,我意识到读取器的函数会为此类文件返回一个 NULL 文件指针。奇怪的是,这些文件存在于程序的工作目录中并且没有损坏。

我进一步提供了相关的代码 block :

我的主要调用者代码:

int main(){
int stat; //Identifier of RINEX file status.

//Memory allocation for a "stat_t" variable (initialization of its members will be done when the program flow passes into the "rinex.c" file functions):
sta_t *st = (sta_t *) malloc(sizeof(sta_t));
//Get current working directory (for debugging):
char directory[_MAX_PATH];
_getcwd(directory, sizeof(directory));
printf("Current working directory: %s \n", directory);
//Initialize options structure members:
init_opt_members();
//Initialize processing options structure members:
init_prcopt_members();
//Initialization of observation data structure members (NULL: obs data not available):
init_obs_members();
//Initialization of navigation data structure members (NULL: nav data not available):
init_nav_members();
//Initialization of solution parameters data structure members (NULL: ...):
init_sol_members();

//Read configuration file and update the values of processing options struct:
loadopts("prcopt.txt", opt);
getsysopts(op, NULL, NULL);

//Read RINEX nav file:
stat = readrnx("IOAN.12N", 1, NULL, na, st); //ob = NULL
printf("Reader status = %d \n", stat);

//Read RINEX obs file:
stat = readrnx("IOAN.12O", 1, ob, NULL, st); //na = NULL, st members values will be updated by obs file header data.
printf("Reader status = %d \n", stat);

//Call the single-point positioning function:
stat = pntpos(ob->data, ob->n, na, op, so, NULL, NULL, "Error!");
printf("Single point pos status = %d \n", stat);

system("PAUSE");
return 0;
}

其中 readrnx() 函数(由库提供)是:

extern int readrnx(const char *file, int rcv, obs_t *obs, nav_t *nav, sta_t *sta)
{
gtime_t t={0};

trace(3,"readrnx : file=%s rcv=%d\n",file,rcv);

return readrnxt(file,rcv,t,t,0.0,obs,nav,sta);
}

readrnxt() 在某个时刻调用文件解压缩和打开函数readrnxfile()。这是(根据标准库的实现进行了轻微修改):

static int readrnxfile(const char *file, gtime_t ts, gtime_t te, double tint,
                   int flag, int index, char *type, obs_t *obs, nav_t *nav,
                   sta_t *sta)
{
FILE *fp = NULL;
int cstat,stat;
char tmpfile[1024];

trace(3,"readrnxfile: file=%s flag=%d index=%d\n",file,flag,index);

if (sta) init_sta(sta);

/* uncompress file */
/*if ((cstat=uncompress(file,tmpfile))<0) {
    trace(2,"rinex file uncompact error: %s\n",file);
    return 0;
}*/
/*if (!(fp=fopen(cstat?tmpfile:file,"r"))) {
    trace(2,"rinex file open error: %s\n",cstat?tmpfile:file);
    return 0;
}*/
//It can't open nav files!
if (!(fp=fopen(file,"r"))) {
    trace(2,"rinex file open error: %s\n",file);
    printf("opening file failed: %s\n", strerror(errno)); //For debugging.
    return 0;
}
/* read rinex file */
stat=readrnxfp(fp,ts,te,tint,flag,index,type,obs,nav,sta);

fclose(fp);

/* delete temporary file */
//if (cstat) remove(tmpfile);

return stat;
}

通过打印errno,我发现了上面引用的文件打开错误,该错误仅在上述函数尝试打开导航消息文件时发生。

编辑:忘了说我已经检查了文件权限(我使用的是 Windows 7)并且我使用 Microsoft Visual Studio 2008 professional。

最佳答案

您可以尝试打印出完整的文件名,并检查它是否可用。 另外,您可能需要检查该文件是否可以打开。是不是被其他东西打开了?

关于无法使用 c 的 fopen() 打开放入工作目录的现有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11430051/

相关文章:

c - libusb-1.0 调试信息

c - 数组第二个元素的值未正确打印

gps - 如何使用 Google Maps API 检索高速公路中的运动方向

C 无法在 Unix 中打开文件,但在 macOSx 上运行良好

c++ - 在应用程序崩溃期间获取堆栈跟踪 - C/C++

ios - 什么被弃用了?如何在所有版本中处理它?

安卓室内增强现实

c - malloc 内存损坏,fopen

python - 从经常更新的文件中读取

c - 使用qsort对结构进行排序(按 "last name"字段排序)