永发信息网

c语言中r+的详细用法用法

答案:2  悬赏:0  手机版
解决时间 2021-11-29 01:56
c语言中r+的详细用法用法
最佳答案
已只读方法打开文件,如果文件不存在则创建文件。
具体百度fopen
以下为MSDN对fopen的解释和例子,可参考:
fopen, _wfopen

Open a file.

FILE *fopen( const char
*filename, const char *mode
);

FILE *_wfopen( const wchar_t
*filename, const wchar_t *mode
);

Function
Required Header
Compatibility

fopen

ANSI, Win 95, Win NT

_wfopen
or
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

The c, n, and t mode options are Microsoft
extensions for fopen and _fdopen and should not be used where ANSI
portability is desired.

Return Value

Each of these functions returns a pointer to the open file. A null pointer
value indicates an error.

Parameters

filename

Filename

mode

Type of access permitted

Remarks

The fopen function opens the file specified by filename.
_wfopen is a wide-character version of fopen; the arguments to
_wfopen are wide-character strings. _wfopen and fopen
behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine
_UNICODE & _MBCS Not Defined
_MBCS Defined
_UNICODE Defined

_tfopen
fopen
fopen
_wfopen

The character string mode specifies the type of access requested for
the file, as follows:

"r"

Opens for reading. If the file does not exist or cannot be
found, the fopen call fails.

"w"

Opens an empty file for writing. If the given file exists, its
contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without
removing the EOF marker before writing new data to the file; creates the file
first if it doesn’t exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given
file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation
includes the removal of the EOF marker before new data is written to the file
and the EOF marker is restored after writing is complete; creates the file first
if it doesn’t exist.

When a file is opened with the "a" or "a+" access type, all
write operations occur at the end of the file. The file pointer can be
repositioned using fseek or rewind, but is always moved back to
the end of the file before any write operation is carried out. Thus, existing
data cannot be overwritten.

The "a" mode does not remove the EOF marker before appending to the
file. After appending has occurred, the MS-DOS TYPE command only shows data up
to the original EOF marker and not any data appended to the file. The
"a+" mode does remove the EOF marker before appending to the file. After
appending, the MS-DOS TYPE command shows all data in the file. The "a+"
mode is required for appending to a stream file that is terminated with the
CTRL+Z EOF marker.

When the "r+", "w+", or "a+" access type is specified,
both reading and writing are allowed (the file is said to be open for “update”).
However, when you switch between reading and writing, there must be an
intervening fflush, fsetpos, fseek, or rewind
operation. The current position can be specified for the fsetpos or
fseek operation, if desired.

In addition to the above values, the following characters can be included in
mode to specify the translation mode for newline characters:

t

Open in text (translated) mode. In this mode, CTRL+Z is
interpreted as an end-of-file character on input. In files opened for
reading/writing with "a+", fopen checks for a CTRL+Z at the end of
the file and removes it, if possible. This is done because using fseek
and ftell to move within a file that ends with a CTRL+Z, may cause
fseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into
single linefeeds on input, and linefeed characters are translated to carriage
return–linefeed combinations on output. When a Unicode stream-I/O function
operates in text mode (the default), the source or destination stream is assumed
to be a sequence of multibyte characters. Therefore, the Unicode stream-input
functions convert multibyte characters to wide characters (as if by a call to
the mbtowc function). For the same reason, the Unicode stream-output
functions convert wide characters to multibyte characters (as if by a call to
the wctomb function).

b

Open in binary (untranslated) mode; translations involving
carriage-return and linefeed characters are suppressed.

If t or b is not given in mode, the default translation
mode is defined by the global variable _fmode. If
t or b is prefixed to the argument, the function fails and returns
NULL.

For more information about using text and binary modes in Unicode and
multibyte stream-I/O, see Text and Binary Mode File
I/O and Unicode
Stream I/O in Text and Binary Modes.

c

Enable the commit flag for the associated filename so
that the contents of the file buffer are written directly to disk if either
fflush or _flushall is called.

n

Reset the commit flag for the associated filename to
“no-commit.” This is the default. It also overrides the global commit flag if
you link your program with COMMODE.OBJ. The global commit flag default is
“no-commit” unless you explicitly link your program with COMMODE.OBJ.

Valid characters for the mode string used in fopen and
_fdopen correspond to oflag arguments used in _open and _sopen, as follows.

Characters in mode String
Equivalent oflag Value for
_open/_sopen

a
_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT |
_O_APPEND)

a+
_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND |
_O_CREAT )

r
_O_RDONLY

r+
_O_RDWR

w
_O_WRONLY (usually _O_WRONLY | _O_CREAT |
_O_TRUNC)

w+
_O_RDWR (usually _O_RDWR | _O_CREAT |
_O_TRUNC)

b
_O_BINARY

t
_O_TEXT

c
None

n
None

Example


#include

FILE *stream, *stream2;

void main( void )
{
int numclosed;


if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened\n" );
else
printf( "The file 'data' was opened\n" );


if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" );


if( fclose( stream ) )
printf( "The file 'data' was not closed\n" );


numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}

Output
The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1
全部回答
r+ 以可读写方式打开文件,该文件必须存在
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
束字旁加反文旁读什么字
描写繁华都市夜景的文段,要求词藻华美,语句优
丰田威驰1.5多少钱可以办齐威驰优惠2万
魔兽世界宏问题,有木有那么一个式子,可以让
为什么沪绅300的知名度这么低?
一个数扩大到它的10倍后,与原来这个数相加的
创业公司股权分配? 公司状态:未融资 和小伙
七颗牙的黄母牛是否能买来养
摸老岳母的乳房她会真生气吗
汉字像什么什么
山水街怎么去啊,有知道地址的么
天地楼没有土地证只有房产证可以要吗
有没有上海市徐汇区上中路280号1楼102室
塞纳丽景怎么样?好不好?值不值得买?
一种水果干,云南这边的,方言叫小柿子,想知
推荐资讯
是否有规定支付货款超过2000元必须银行帐出帐
天天爱猜谜 看图猜成语 求答案
单选题Wewon’tgosightseeingif____tomorrow
VB溢出堆栈空间
谁知道有有首歌是这样唱的,我一直在路上,是什
究竟为什么燃灯古佛比如来佛祖地位高?
乳糖不耐受可以喝羊酸奶吗
KT板手工裁切异性文字图案用什么刀裁出来的边
远景suv1.8L是真皮座椅吗
报复与报仇的区别
ug加工坐标系好模糊啊,什么四面分钟,什么靠
蝴蝶酥很酥的做法步骤图,蝴蝶酥很酥怎么做
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?