当前位置: 技术问答>linux和unix
linux c 下的文件操作
来源: 互联网 发布时间:2016-07-17
本文导语: 在linux c 下是否能够使用fopen 和fclose 两个函数打开文件?为什么我使用fopen是程序出现“core dump”的错误,而使用open却可以成功。因为我接下来的工作用fopen比较方便,所以请高手指教。 还有是不是所有vc下的c函...
在linux c 下是否能够使用fopen 和fclose 两个函数打开文件?为什么我使用fopen是程序出现“core dump”的错误,而使用open却可以成功。因为我接下来的工作用fopen比较方便,所以请高手指教。
还有是不是所有vc下的c函数都能够在linux c下使用?
还有是不是所有vc下的c函数都能够在linux c下使用?
|
C语言在Linux下面是完全可以用这两个函数的,可能是没有连接好函数库!
|
肯定可以,标准C的。
关键是你代码其他地方有错误操作。
关键是你代码其他地方有错误操作。
|
study
|
描述符 定义错了吧?
|
fopen 需要返回一个FILE*类型,而open是返回一个int类型,楼主应该是文件描述符定义的类型不对。如果用int类型去获取fopen的返回应该就会出错。
|
如果是内核方面的开发不能使用标准C库文件,
如果是作应用,要确定fopen()函数的返回值
如果是作应用,要确定fopen()函数的返回值
|
linux下当然可以使用fopen和fclose,你出错的程序贴上来看看?
|
1. 确认是 fopen就错了,还是之后,比如open一个不存在的文件,然后fread就会错
2. 确认是否是应用程序,在驱动里不能用fopen
2. 确认是否是应用程序,在驱动里不能用fopen
|
linux c 下是能够使用fopen 和fclose 两个函数打开文件, 出现错误, 查一下参数有没有对.
还有是不是所有vc下的c函数都能够在linux c下使用?
ANSI C 的库函数都能够在 Linux下使用.
还有是不是所有vc下的c函数都能够在linux c下使用?
ANSI C 的库函数都能够在 Linux下使用.
|
#include
FILE *fopen(const char *path, const char *mode);
The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
FILE *fopen(const char *path, const char *mode);
The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
r Open text file for reading. The stream is positioned at the beginning of the file.
r+ Open for reading and writing. The stream is positioned at the beginning of the file.
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
|
仔细比对,是不是自己函数某个参数错了。导致这个问题的情况很多。把代码贴上来,大家共赏。
|
fopen和open最主要的区别是fopen在用户态下就有了缓存,在进行read和write的时候减少了用户态和内核态的切换,而open则每次都需要进行内核态和用户态的切换;表现为,如果顺序访问文件,fopen系列的函数要比直接调用open系列快;如果随机访问文件open要比fopen快。
|
还是建议和程序贴出来看看!
|
理论上都可以的,你还是上代码吧!
尽管在linux里面所有的设备都可以当做一个文件来看待,不过使用习惯上还是有些差别
尽管在linux里面所有的设备都可以当做一个文件来看待,不过使用习惯上还是有些差别