
C语言关于文件操作的简单问题!!
问题:创建一个新文件,如果已存在与待创建的文件同名的文件则询问用户是否覆盖------------------------------------------------...
问题:创建一个新文件,如果已存在与待创建的文件同名的文件则询问用户是否覆盖
-----------------------------------------------------
我不明白的是怎么判断一个文件是否已存在(用fopen以读方式打开文件来判断文件是否存在有缺陷)
还有一个简单的问题,如何对文件进行部分擦写,如将一个存有
字符串"abbbbbba"的文件中间的字符b消掉,要求是在原文件的基础上操作
------------------------
用fopen("","r");的方法碰上没有读权限时就会出错,另外,如果要对文件进行写操作就必须关掉文件再重新打开,此期间如果有其它程序创建了此文件也会出错,所以这个方法不可取
回外围的爱 :fopen(""","a");方法怎么处理插入和删除操作啊??不会每次都要把后面的东西读出来,改完再写吧?
回zf1987013:麻烦介绍一本有这方面的资料的书好吗?我确实找不到 展开
-----------------------------------------------------
我不明白的是怎么判断一个文件是否已存在(用fopen以读方式打开文件来判断文件是否存在有缺陷)
还有一个简单的问题,如何对文件进行部分擦写,如将一个存有
字符串"abbbbbba"的文件中间的字符b消掉,要求是在原文件的基础上操作
------------------------
用fopen("","r");的方法碰上没有读权限时就会出错,另外,如果要对文件进行写操作就必须关掉文件再重新打开,此期间如果有其它程序创建了此文件也会出错,所以这个方法不可取
回外围的爱 :fopen(""","a");方法怎么处理插入和删除操作啊??不会每次都要把后面的东西读出来,改完再写吧?
回zf1987013:麻烦介绍一本有这方面的资料的书好吗?我确实找不到 展开
4个回答
展开全部
若
fopen(filename, "r") == NULL
则文件不存在。没什么副作用。这里其实可能是文件不存在,也可能是无读取权限,不过效果差不多。
如果实在要区分是读取失败是不是由文件不存在导致的,就必须用操作系统提供的系统函数了(在Windows就是API函数),请另查手册。
如果用"r"方式打开文件后要进行写操作,可再用freopen()函数。和关了再打开差不多。
修改已有的文件只要用"a+"或"r+"打开文件,用fscanf()等的进行空读,找到内容再写。
问题主要是文件定位问题。可用ftell()、rewind()、fseek()等函数。
不过一般要进行修改的文件都用二进制文件,内容按确定形式存储,这样方便定位和修改。
注意:插入和删除文件的一部分内容总是很困难的,一般的实现(比如我们常用的文字处理软件)都是把所有内容读入内存,改好后又一股脑儿写到磁盘上。否则磁盘读写相对效率太低,是不能接受的。
下面附The C Programming Language中的一些说明。抱歉只有英文。
// fopen()函数
FILE *fopen(const char *filename, const char *mode)
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r" open text file for reading
"w" create text file for writing; discard previous contents if any
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e., reading and writing)
"w+" create text file for update, discard previous contents if any
"a+" append; open or create text file for update, writing at end
Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
// freopen()函数
FILE *freopen(const char *filename, const char *mode, FILE *stream)
freopen opens the file with the specified mode and associates the stream with it. It returns stream, or NULL if an error occurs. freopen is normally used to change the files associated with stdin, stdout, or stderr.
// fseek()函数,用于各种定位
int fseek(FILE *stream, long offset, int origin)
fseek sets the file position for stream; a subsequent read or write will access data beginning at the new position. For a binary file, the position is set to offset characters from origin, which may be SEEK_SET (beginning), SEEK_CUR (current position), or SEEK_END (end of file). For a text stream, offset must be zero, or a value returned by ftell (in which case origin must be SEEK_SET). fseek returns non-zero on error.
// ftell()函数,用于获得位置(可以用一个变量记下,方便返回)
long ftell(FILE *stream)
ftell returns the current file position for stream, or -1 on error.
// 定位至文件头,这个可能对修改文件最有用
void rewind(FILE *stream)
rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); clearerr(fp).
// 获得位置,注意这个主要用于二进制文件
int fgetpos(FILE *stream, fpos_t *ptr)
fgetpos records the current position in stream in *ptr, for subsequent use by fsetpos. The type fpos_t is suitable for recording such values. fgetpos returns non-zero on error.
// 位置跳转,用于二进制文件
int fsetpos(FILE *stream, const fpos_t *ptr)
fsetpos positions stream at the position recorded by fgetpos in *ptr. fsetpos returns non-zero on error.
fopen(filename, "r") == NULL
则文件不存在。没什么副作用。这里其实可能是文件不存在,也可能是无读取权限,不过效果差不多。
如果实在要区分是读取失败是不是由文件不存在导致的,就必须用操作系统提供的系统函数了(在Windows就是API函数),请另查手册。
如果用"r"方式打开文件后要进行写操作,可再用freopen()函数。和关了再打开差不多。
修改已有的文件只要用"a+"或"r+"打开文件,用fscanf()等的进行空读,找到内容再写。
问题主要是文件定位问题。可用ftell()、rewind()、fseek()等函数。
不过一般要进行修改的文件都用二进制文件,内容按确定形式存储,这样方便定位和修改。
注意:插入和删除文件的一部分内容总是很困难的,一般的实现(比如我们常用的文字处理软件)都是把所有内容读入内存,改好后又一股脑儿写到磁盘上。否则磁盘读写相对效率太低,是不能接受的。
下面附The C Programming Language中的一些说明。抱歉只有英文。
// fopen()函数
FILE *fopen(const char *filename, const char *mode)
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r" open text file for reading
"w" create text file for writing; discard previous contents if any
"a" append; open or create text file for writing at end of file
"r+" open text file for update (i.e., reading and writing)
"w+" create text file for update, discard previous contents if any
"a+" append; open or create text file for update, writing at end
Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
// freopen()函数
FILE *freopen(const char *filename, const char *mode, FILE *stream)
freopen opens the file with the specified mode and associates the stream with it. It returns stream, or NULL if an error occurs. freopen is normally used to change the files associated with stdin, stdout, or stderr.
// fseek()函数,用于各种定位
int fseek(FILE *stream, long offset, int origin)
fseek sets the file position for stream; a subsequent read or write will access data beginning at the new position. For a binary file, the position is set to offset characters from origin, which may be SEEK_SET (beginning), SEEK_CUR (current position), or SEEK_END (end of file). For a text stream, offset must be zero, or a value returned by ftell (in which case origin must be SEEK_SET). fseek returns non-zero on error.
// ftell()函数,用于获得位置(可以用一个变量记下,方便返回)
long ftell(FILE *stream)
ftell returns the current file position for stream, or -1 on error.
// 定位至文件头,这个可能对修改文件最有用
void rewind(FILE *stream)
rewind(fp) is equivalent to fseek(fp, 0L, SEEK_SET); clearerr(fp).
// 获得位置,注意这个主要用于二进制文件
int fgetpos(FILE *stream, fpos_t *ptr)
fgetpos records the current position in stream in *ptr, for subsequent use by fsetpos. The type fpos_t is suitable for recording such values. fgetpos returns non-zero on error.
// 位置跳转,用于二进制文件
int fsetpos(FILE *stream, const fpos_t *ptr)
fsetpos positions stream at the position recorded by fgetpos in *ptr. fsetpos returns non-zero on error.
展开全部
可以用fopen("文件名","r")方式打开,来判断吧,如果文件不存在,则出错。
擦写则可以以fopen("文件名"","a")的方式来打开再擦写就可以了吧。
擦写则可以以fopen("文件名"","a")的方式来打开再擦写就可以了吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C还真不知道 但C++文件流操作有一个属性几是ios::后面写的(没查书.忘了)就是判断文件是否存在,如果不存在的话就不建立新的文件!!用这个判断就比较好了!但是C语言的还真不知道!!查一下!也会有吧!!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
几乎每一本书上都有说明的啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询