c语言fopen函数 fp=fopen(" /","r") fopen 函数怎么样打开url 文件 比如 http://10.4.64.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言fopen函数 fp=fopen(" /","r") fopen 函数怎么样打开url 文件 比如 http://10.4.64.相关的知识,希望对你有一定的参考价值。
fopen函数用来打开一个文件,其调用的一般形式为:文件指针名=fopen(文件名,使用文件方式);
其中,
“文件指针名”必须是被说明为FILE 类型的指针变量;
“文件名”是被打开文件的文件名;
“使用文件方式”是指文件的类型和操作要求。
“文件名”是字符串常量或字符串数组。
例如:
FILE *fp;
fp=("file a","r");
其意义是在当前目录下打开文件file a,只允许进行“读”操作,并使fp指向该文件。
又如:
FILE *fphzk
fphzk=("c:\\hzk16","rb")
其意义是打开C驱动器磁盘的根目录下的文件hzk16,这是一个二进制文件,只允许按二进制方式进行读操作。两个反斜线“\\ ”中的第一个表示转义字符,第二个表示根目录。
使用文件的方式共有12种,下面给出了它们的符号和意义。
文件使用方式
意义
“rt”
只读打开一个文本文件,只允许读数据
“wt”
只写打开或建立一个文本文件,只允许写数据
“at”
追加打开一个文本文件,并在文件末尾写数据
“rb”
只读打开一个二进制文件,只允许读数据
“wb”
只写打开或建立一个二进制文件,只允许写数据
“ab”
追加打开一个二进制文件,并在文件末尾写数据
“rt+”
读写打开一个文本文件,允许读和写
“wt+”
读写打开或建立一个文本文件,允许读写
“at+”
读写打开一个文本文件,允许读,或在文件末追加数据
“rb+”
读写打开一个二进制文件,允许读和写
“wb+”
读写打开或建立一个二进制文件,允许读和写
“ab+”
读写打开一个二进制文件,允许读,或在文件末追加数据
对于文件使用方式有以下几点说明:
1) 文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:
r(read): 读
w(write): 写
a(append): 追加
t(text): 文本文件,可省略不写
b(banary): 二进制文件
+: 读和写
2) 凡用“r”打开一个文件时,该文件必须已经存在,且只能从该文件读出。
3) 用“w”打开的文件只能向该文件写入。若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件。
4) 若要向一个已存在的文件追加新的信息,只能用“a”方式打开文件。但此时该文件必须是存在的,否则将会出错。
5) 在打开一个文件时,如果出错,fopen将返回一个空指针值NULL。在程序中可以用这一信息来判别是否完成打开文件的工作,并作相应的处理。因此常用以下程序段打开文件:
6) if((fp=fopen("c:\\hzk16","rb")==NULL)
printf("\nerror on open c:\\hzk16 file!");
getch();
exit(1);
这段程序的意义是,如果返回的指针为空,表示不能打开C盘根目录下的hzk16文件,则给出提示信息“error on open c:\ hzk16 file!”,下一行getch()的功能是从键盘输入一个字符,但不在屏幕上显示。在这里,该行的作用是等待,只有当用户从键盘敲任一键时,程序才继续执行,因此用户可利用这个等待时间阅读出错提示。敲键后执行exit(1)退出程序。
7) 把一个文本文件读入内存时,要将ASCII码转换成二进制码,而把文件以文本方式写入磁盘时,也要把二进制码转换成ASCII码,因此文本文件的读写要花费较多的转换时间。对二进制文件的读写不存在这种转换。
8) 标准输入文件(键盘),标准输出文件(显示器),标准出错输出(出错信息)是由系统打开的,可直接使用。
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela 参考技术A 百度一下吧!
在VC++中用fopen
在一个程序中重写了OnSaveDocument函数,如下:
BOOL CDrawDoc::OnSaveDocument(LPCTSTR lpszPathName)
FILE *fp;
fp = fopen(lpszPathName,"w+b");
fwrite(this,sizeof(*this),1,fp);
fclose(fp);
return CDocument::OnSaveDocument(lpszPathName);
然后编译的时候老是提示什么
\DrawDoc.cpp(361) : error C2664: “fopen”: 不能将参数 1 从“LPCTSTR”转换为“const char *”
与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
是什么意思啊...为什么我看别人的程序又没有这个问题...郁闷
LPCTSTR就表示一个指向常固定地址的可以根据一些宏定义改变语义的字符串。
百科的解释,而且那个参数是系统默认的
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 <stdio.h> ANSI, Win 95, Win NT
_wfopen <stdio.h> or <wchar.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
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
/* FOPEN.C: This program opens files named "data"
* and "data2".It uses fclose to close "data" and
* _fcloseall to close all remaining files.
*/
#include <stdio.h>
FILE *stream, *stream2;
void main( void )
int numclosed;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened\\n" );
else
printf( "The file 'data' was opened\\n" );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\\n" );
else
printf( "The file 'data2' was opened\\n" );
/* Close stream */
if( fclose( stream ) )
printf( "The file 'data' was not closed\\n" );
/* All other files are closed: */
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 参考技术A fopen要的参数是const char*,而LPCTSTR可能是宽字符(UNICODE),你在工程属性里改一下,不用UNICODE就行了。
另,既然用了MFC就用CFile读写文件,不建议用fopen。本回答被提问者采纳 参考技术B LPCTSTR lpszPathName
你这里传递过来的是文件名? 那么文件名简单的说应该是存放在一个字符串当中了 不知道你的LPCTSTR的意义是什么 参考技术C = =||
童鞋,文件名要用"" 括起来
foen的格式是 fopen("file name","someway");
第一个双引号括起来的是文件目录,例如"D:\游戏\游戏"
也可以只写文件名,这样的话,如果你是读文件的话,要保证你要读的文件在默认目录下,就是你放源代码的目录,而如果你是 w (写入)一个文件的话,新建的文件就会在默认目录下。
第二个双引号括起来的是你的打开方式,w 为新建一个文件,并且写入内容, r 的话就是读入
如果在目录下找不到文件的话,就会打开失败。
以上我相信你的学习课本上应该都有的,认真看看吧。
以上是关于c语言fopen函数 fp=fopen(" /","r") fopen 函数怎么样打开url 文件 比如 http://10.4.64.的主要内容,如果未能解决你的问题,请参考以下文章