求教 文件指针问题!

withwind99 2003-10-16 06:14:52
#include <stdio.h>
#include <string.h>

main()
{
char *str1 = "guo";
char* ptr;
FILE * fp;
char *str;

if((fp=fopen("db","rb"))==NULL)
{
printf("can not open");

}
str=(char*)fp;
ptr = strstr(str, str1);
printf("The substring is: %s\n", ptr);

}

db是一个文本文件,运行是为什么打不开呀,还有fp的类型转换和strstr的用法对 吗,希望各位多多指教!!!!!

...全文
54 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharkhuang 2003-10-19
  • 打赏
  • 举报
回复
fdopen()
可以把文件指针转换成stream!!!

非常好的函数!
playboyxp 2003-10-19
  • 打赏
  • 举报
回复
从没见过有这样写得
文件指针可以转换成字符形得
但得到得不是字符指针得效果
我的女神 2003-10-19
  • 打赏
  • 举报
回复
if((fp=fopen("db","rb"))==NULL)
{
printf("can not open");

}
我只看到这,然后看了看你的问题:
把db写成db.txt试试,前提是你的程序可以运行!
TianGuangZao 2003-10-18
  • 打赏
  • 举报
回复
楼主的程序很有想象力!
经楼主点播,试验了一番:

#include <stdio.h>
#include <string.h>

main()
{
char *str1 = "guo";
char *ptr;
FILE *fp;
char *str;

if ((fp = fopen("db", "rb") ) == NULL)
{
printf("can not open");
return -1;
}
/* 大伙把一下几个都试试看,还可以多试验几个,只要带读文件动作的函数都可以,
实际应该是写入内存缓冲区 */
//fseek (fp, 0, SEEK_SET);
//fseek (fp, 0, SEEK_END);
fseek (fp, 20, SEEK_SET); /* 改动数字看看结果 */
//rewind (fp);

str = fp->_IO_read_base; /* 需要对 FILE 结构有一定了解 */
ptr = strstr( str, str1);
printf( "The substring is: %s\n", ptr);

fclose (fp);
return 0;
}

stdio.h:
typedef struct _IO_FILE FILE;

libio.h:
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _fileno;
#if 0
int _blksize;
#else
int _flags2;
#endif
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */

#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];

/* char* _save_gptr; char* _save_egptr; */

_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

struct _IO_FILE_complete
{
struct _IO_FILE _file;
#endif
#if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
_IO_off64_t _offset;
# if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
/* Wide character stream stuff. */
struct _IO_codecvt *_codecvt;
struct _IO_wide_data *_wide_data;
# else
void *__pad1;
void *__pad2;
# endif
int _mode;
/* Make sure we don't get into trouble again. */
char _unused2[15 * sizeof (int) - 2 * sizeof (void *)];
#endif
};
RichJ 2003-10-18
  • 打赏
  • 举报
回复
牛就一个字!
这样的代码不容易写!
langzi8818 2003-10-18
  • 打赏
  • 举报
回复
偶也没有看见过怎么做的,看眼界啊
herodazhu 2003-10-18
  • 打赏
  • 举报
回复
强烈同意楼上的观点!
hackingtruth 2003-10-18
  • 打赏
  • 举报
回复
强制类型转换不对。指向文件的指针不能修改。
zoutianshighost 2003-10-18
  • 打赏
  • 举报
回复
文件指针是指向一个储存文件信息结构体的,不能把它想成一个字符符流,或字符长串。所以不能用(char*)fp。
还有strstr()是什么?我没见过, 应该是substr()吧
ssbull 2003-10-16
  • 打赏
  • 举报
回复
文件打不开,有几个可能:
1.文件路径不对。
2.文件名不对,你看看你是不是在定义文件名时,写的是db.txt,有可能你自己多写了扩展名,使文件名变成db.txt.txt,这样也会打不开文件。
对于你的转换str=(char*)fp;我也从没看到过这么做,这样你改变了fp的类型,你还怎么操作你的文件读写呀?
program2100 2003-10-16
  • 打赏
  • 举报
回复
$$ str=(char*)fp; $$
没见过这样转换的

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧