在VS2008的Debug32模式和Dos下运行程序时对于“./”的理解

来源:互联网 发布:物性参数查询软件 编辑:程序博客网 时间:2024/05/21 12:07
 本文要比较的是在VS2008Debug32模式和Dos下运行程序时对于“./”的理解。

如下所示,是一个简单的C/C++程序:

  1. #include <stdio.h>
  2. #include <tchar.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.     FILE* fpr;
  9.     char msgFile[80];
  10.     char dir[80];
  11.     char file[80];
  12.     char buf[80];
  13.     memset(msgFile, '/0', 80);
  14.     memset(dir, '/0', 80);
  15.     memset(file, '/0', 80);
  16.     memset(buf, '/0', 80);
  17.     GetPrivateProfileString("TEST""MSG_DIR""F://vs2008//Test", (LPTSTR)dir, 80, "F://vs2008//Test//Test.ini");
  18.     GetPrivateProfileString("TEST""MSG_FILE""message.dat", (LPTSTR)file, 80, "F://vs2008//Test//Test.ini");
  19.     strcpy_s(msgFile, 80, dir);
  20.     strcat_s(msgFile, 80, "//");
  21.     strcat_s(msgFile, 80, file);
  22.     if(!fopen_s(&fpr, msgFile, "r")){
  23.         fgets(buf, 80, fpr);
  24.         printf_s("%s/n", buf);
  25.     }
  26.     return 0;
  27. }

其中的Test.ini文件如下:

[TEST]

MSG_DIR=F:/vs2008/Test

MSG_FILE=message.dat

message.dat文件如下:

This is a test program!

上面的程序运行的结果如图1所示:

 

 

先对程序和Test.ini文件做如下修改:

程序:

GetPrivateProfileString("TEST", "MSG_DIR", ".", (LPTSTR)dir, 80, "F://vs2008//Test//Test.ini");

ini文件:

;MSG_DIR=F:/vs2008/Test

(前面加了一个分号,即注释这一行)

做上述改动后,在程序中msgFile的值就变成了“./message.dat”,此时运行程序得到的结果如图2所示:

 

 

也就是说,这时程序已经找不到message.dat文件了,那如何才能让程序找到message.dat文件呢?

你可以将message.dat文件放在F:/vs2008/Test/Test目录下,即将它和Test.cpp放在一起。

此时你如果再运行改过之后的程序运行结果就如图1所示了。

 

这时,我们不妨再做一个尝试,就是在dos命令行格式中运行Test.exe,如图3所示:

 

 

从图中可以看出,没有输出,也就是说,message.dat文件又找不到了。

这时,如果你将message.dat文件放在F:/vs2008/Test/Debug文件夹下,则它又能访问了,如图4所示:

 

原创粉丝点击