user-0 提问: fopen函数问题

回复:
user: user-1
content: 引用 5 楼 lgbxyz 的回复:文件路径不对,既不是linux的也不是window是的  char * FILENAME = "G:/test/cfg2.txt"; //应该是下面这个吧 char * FILENAME = "G:\\test\\cfg2.txt";  win下双'\'转义,应该是这样的

user: user-2
content: 文件路径不对,既不是linux的也不是window是的  char * FILENAME = "G:/test/cfg2.txt"; //应该是下面这个吧 char * FILENAME = "G:\\test\\cfg2.txt";

user: user-3
content: _chdir, _wchdir Change the current working directory.  int _chdir( const char *dirname );  int _wchdir( const wchar_t *dirname );  Routine Required Header Optional Headers Compatibility  _chdir <direct.h> <errno.h> Win 95, Win NT  _wchdir <direct.h> or <wchar.h> <errno.h> Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Each of these functions returns a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT.  Parameter  dirname  Path of new working directory  Remarks  The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:  _chdir("c:\\temp");  When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\).  _wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise.  Generic-Text Routine Mapping:  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tchdir _chdir _chdir _wchdir    Example  /* CHGDIR.C: This program uses the _chdir function to verify  * that a given directory exists.  */  #include <direct.h> #include <stdio.h> #include <stdlib.h>  void main( int argc, char *argv[] ) {    if( _chdir( argv[1] )   )       printf( "Unable to locate the directory: %s\n", argv[1] );    else       system( "dir *.wri"); }   Output   Volume in drive C is CDRIVE  Volume Serial Number is 0E17-1702   Directory of C:\write  04/21/95  01:06p                 3,200 ERRATA.WRI 04/21/95  01:06p                 2,816 README.WRI                2 File(s)          6,016 bytes                              71,432,116 bytes free   Directory Control Routines  See Also   _mkdir, _rmdir, system

user: user-4
content: 雖然没有一眼看出問題，但建議可以從以下幾個點定位： 1、對於fopen中的filepath， 因為很少在windows下編碼讀取文件，不确信，這裡路径是否写法有問題。 2、從代碼中看，應該是win平臺的。可以参照linux的error code。在打失敗後把error code打出，对應去查看error code的代表含義，這樣可以很快定位出來問题。  // Reset errno to zero before calling fopen().     errno = 0;      // Lets open a file that does not exist        fd = fopen("Linux.txt","r");     if(errno || (NULL == fd))     {         printf("\n fopen() failed， error code =%d\n", errno);     }  对于errno代表含义可以查對應平台的API接口說明。

user: user-0
content: 引用 2 楼 user-4 的回复:雖然没有一眼看出問題，但建議可以從以下幾個點定位： 1、對於fopen中的filepath， 因為很少在windows下編碼讀取文件，不确信，這裡路径是否写法有問題。 2、從代碼中看，應該是win平臺的。可以参照linux的error code。在打失敗後把error code打出，对應去查看error code的代表含義，這樣可以很快定位出來問题。  // Reset errno to zero before calling fopen().     errno = 0;      // Lets open a file that does not exist        fd = fopen("Linux.txt","r");     if(errno || (NULL == fd))     {         printf("\n fopen() failed， error code =%d\n", errno);     }  对于errno代表含义可以查對應平台的API接口說明。 errno = 0; 	fp = fopen(fileName, "wb"); 	  	if (errno || fp == NULL); 	{  		rv = -2; 		printf("func fopen err: %d\n", rv); 		printf("errn0 = %d", errno); 		goto End; 	}   然后呢？

user: user-0
content: 实现函数的文件：(cfg_op.c)中，36行，fp一直等于空，不知道为什么，希望大神们，帮帮忙，小女感激不尽，实现的是一个配置文件的读，没有实现写，文件在磁盘上都新建好了，并且有数据，谢谢大家了