boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - FILE*的问题
相关主题
C语言程序静态库和动态库的创建及其应用
New C++ programmer, need to ask a I/O file read question
c++产生随机数
[合集] Why it only write the file once?
帮忙看看这几段程序有问题吗?
c的问题
C++ class template specialization question
请教一个C++问题
老年工程师转行学C++的更新的问题
help with PHP programming!!! (转载)
相关话题的讨论汇总
话题: file话题: fp话题: obj2话题: obj1话题: read
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
如果单线程程序,两个对象共享一个FILE*指针会有问题吗?
比如每个对象都有个_fp私有成员,某个对象打开文件时做:
FILE* fp = fopen("foo.txt", r);
obj1->set_fp(fp);
obj2->set_fp(fp);
obj1->read_info(offset1);
// move file pointer to offset1 and read from file foo.txt.
obj2->read_info(offset2);
...
fclose(fp);
obj1->set_fp(NULL);
obj2->set_fp(NULL);
P********e
发帖数: 2610
2
"r"是没问题的

【在 g*********s 的大作中提到】
: 如果单线程程序,两个对象共享一个FILE*指针会有问题吗?
: 比如每个对象都有个_fp私有成员,某个对象打开文件时做:
: FILE* fp = fopen("foo.txt", r);
: obj1->set_fp(fp);
: obj2->set_fp(fp);
: obj1->read_info(offset1);
: // move file pointer to offset1 and read from file foo.txt.
: obj2->read_info(offset2);
: ...
: fclose(fp);

m*****e
发帖数: 4193
3

Yes.

【在 g*********s 的大作中提到】
: 如果单线程程序,两个对象共享一个FILE*指针会有问题吗?
: 比如每个对象都有个_fp私有成员,某个对象打开文件时做:
: FILE* fp = fopen("foo.txt", r);
: obj1->set_fp(fp);
: obj2->set_fp(fp);
: obj1->read_info(offset1);
: // move file pointer to offset1 and read from file foo.txt.
: obj2->read_info(offset2);
: ...
: fclose(fp);

t****t
发帖数: 6806
4
you can do that. however beware, it's dangerous to mix FILE operations. for
example, you must do a seek between read and write; if you do read in one
and write in another one, you could miss the seek.

【在 g*********s 的大作中提到】
: 如果单线程程序,两个对象共享一个FILE*指针会有问题吗?
: 比如每个对象都有个_fp私有成员,某个对象打开文件时做:
: FILE* fp = fopen("foo.txt", r);
: obj1->set_fp(fp);
: obj2->set_fp(fp);
: obj1->read_info(offset1);
: // move file pointer to offset1 and read from file foo.txt.
: obj2->read_info(offset2);
: ...
: fclose(fp);

r*********r
发帖数: 3195
5
c++ 里用 FILE * 太底层了.
中间随便哪个地方出个 exception, fclose 就到不了.
1 (共1页)
进入Programming版参与讨论
相关主题
help with PHP programming!!! (转载)
fprintf in C\C++
请问如何恢复正常的IO?
Question of building apache module.
Path with non-ascii character
求助:关于指针和数据存储
求助:关于文件夹和文件的读写
open an image file
a C/C++ fopen mode question
怎样读一个不断更新的文件
相关话题的讨论汇总
话题: file话题: fp话题: obj2话题: obj1话题: read