首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++中不能访问文件解决思路

2014-01-01 
c++中不能访问文件sprintf(filename, %s\\%s%03d.dat,filepath,filetwo ,num)if((access(filename,0))

c++中不能访问文件
sprintf(filename, "%s\\%s%03d.dat",filepath,filetwo ,num);
   if((access(filename,0))== -1) {
printf("Cann't access the file. Error : %s\n",filename);
   }
程序如上所示,断点在第一行,显示filename是正确的,但是就是不能访问文件,不知道为什么。
求教,谢谢了~
[解决办法]
你的第二个参数对不对啊,为啥用0呢?
mode:访问模式。可以F_OK,或者是F_OK和R_OK, W_OK,and X_OK的或(
[解决办法]
)。F_OK,表示pathname所指向文件是否存在。
如果是判断是否存在,用F_OK就行。
[解决办法]
access第二个参数传递0是只检查是不是存在,这个没什么问题。
但是照理说如果文件确实存在,应该不会返回-1,
你再确认是文件名吧。
[解决办法]
_open, _wopen
Open a file.

int _open( const char *filename, int oflag [, int pmode] );

int _wopen( const wchar_t *filename, int oflag [, int pmode] );

Routine Required Header Optional Headers Compatibility 
_open <io.h> <fcntl.h>, <sys/types.h>, <sys/stat.h> Win 95, Win NT 
_wopen <io.h> or <wchar.h> <fcntl.h>, <sys/types.h>, <sys/stat.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 file handle for the opened file. A return value of –1 indicates an error, in which case errno is set to one of the following values:

EACCES

Tried to open read-only file for writing, or file’s sharing mode does not allow specified operations, or given path is directory

EEXIST

_O_CREAT and _O_EXCL flags specified, but filename already exists

EINVAL

Invalid oflag or pmode argument 

EMFILE

No more file handles available (too many open files)

ENOENT

File or path not found

Parameters

filename

Filename

oflag

Type of operations allowed

pmode

Permission mode

Remarks

The _open function opens the file specified by filename and prepares the file for reading or writing, as specified by oflag. _wopen is a wide-character version of _open; the filename argument to _wopen is a wide-character string. _wopen and _open behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_topen _open _open _wopen 


oflag is an integer expression formed from one or more of the following manifest constants or constant combinations defined in FCNTL.H:

_O_APPEND

Moves file pointer to end of file before every write operation.

_O_BINARY

Opens file in binary (untranslated) mode. (See fopen for a description of binary mode.)

_O_CREAT

Creates and opens new file for writing. Has no effect if file specified by filename exists. pmode argument is required when _O_CREAT is specified.

_O_CREAT 
[解决办法]
 _O_SHORT_LIVED

Create file as temporary and if possible do not flush to disk. pmode argument is required when _O_CREAT is specified.

_O_CREAT 
------解决方案--------------------


 _O_TEMPORARY

Create file as temporary; file is deleted when last file handle is closed. pmode argument is required when _O_CREAT is specified. 

_O_CREAT 
[解决办法]
 _O_EXCL

Returns error value if file specified by filename exists. Applies only when used with _O_CREAT.

_O_RANDOM

Specifies primarily random access from disk

_O_RDONLY

Opens file for reading only; cannot be specified with _O_RDWR or _O_WRONLY.

_O_RDWR

Opens file for both reading and writing; you cannot specify this flag with _O_RDONLY or _O_WRONLY.

_O_SEQUENTIAL

Specifies primarily sequential access from disk

_O_TEXT

Opens file in text (translated) mode. (For more information, see Text and Binary Mode File I/O and fopen.)

_O_TRUNC

Opens file and truncates it to zero length; file must have write permission. You cannot specify this flag with _O_RDONLY. _O_TRUNC used with _O_CREAT opens an existing file or creates a new file.

Warning   The _O_TRUNC flag destroys the contents of the specified file.

_O_WRONLY

Opens file for writing only; cannot be specified with _O_RDONLY or _O_RDWR.

To specify the file access mode, you must specify either _O_RDONLY, _O_RDWR, or _O_WRONLY. There is no default value for the access mode.

When two or more manifest constants are used to form the oflag argument, the constants are combined with the bitwise-OR operator ( 
[解决办法]
 ). See Text and Binary Mode File I/O for a discussion of binary and text modes. 

The pmode argument is required only when _O_CREAT is specified. If the file already exists, pmode is ignored. Otherwise, pmode specifies the file permission settings, which are set when the new file is closed the first time. _open applies the current file-permission mask to pmode before setting the permissions (for more information, see _umask). pmode is an integer expression containing one or both of the following manifest constants, defined in SYS\STAT.H: 

_S_IREAD

Reading only permitted

_S_IWRITE

Writing permitted (effectively permits reading and writing)

_S_IREAD 
[解决办法]
 _S_IWRITE

Reading and writing permitted

When both constants are given, they are joined with the bitwise-OR operator ( 
[解决办法]
 ). In Windows NT, all files are readable, so write-only permission is not available; thus the modes _S_IWRITE and _S_IREAD 
[解决办法]
 _S_IWRITE are equivalent.

Example

/* OPEN.C: This program uses _open to open a file
 * named OPEN.C for input and a file named OPEN.OUT
 * for output. The files are then closed.
 */

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdio.h>

void main( void )
{
   int fh1, fh2;

   fh1 = _open( "OPEN.C", _O_RDONLY );
   if( fh1 == -1 )
      perror( "open failed on input file" );
   else
   {
      printf( "open succeeded on input file\n" );
      _close( fh1 );
   }

   fh2 = _open( "OPEN.OUT", _O_WRONLY 
[解决办法]
 _O_CREAT, _S_IREAD 
[解决办法]
 
                            _S_IWRITE );


   if( fh2 == -1 )
      perror( "Open failed on output file" );
   else
   {
      printf( "Open succeeded on output file\n" );
      _close( fh2 );
   }
}


Output

Open succeeded on input file
Open succeeded on output file


Low-Level I/O Routines

See Also   _chmod, _close, _creat, _dup, fopen, _sopen

热点排行