linux 清理缓存问题。

yangboazaaza 2009-10-20 08:31:41
#include "stdlib.h"
#include "stdio.h"
#include "errno.h"
#include "string.h"
#include "netdb.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "netinet/in.h"
#include "sys/socket.h"

void client(int ch)
{
int i;
printf("%d",ch);
switch(ch)
{
/********传输文件******/
case 1: _i = flushall();
send();
break;
/********聊天*********/
case 2:
/*********群聊********/
case 3: break;
}
}
/********************************************************************************************
*我在另外一个函数调用了client 函数,然后传过来一个ch,我想在调用_send()函数之前,清理下缓存区,
*因为我在_send函数里还要输入其他变量的值,
* 'flushall'的头文件不是stdio.h吗?我已经包含了。
*为什么提示我连接错误阿?
* undefined reference to `flushall'
*********************************************************************************************/
...全文
861 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Steven_0610 2009-10-21
  • 打赏
  • 举报
回复
up
liangyonglou 2009-10-21
  • 打赏
  • 举报
回复
一般使用rewind(stdin)
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
哦,看来我也得弄一个,哈。不过能帮我看看我的代码吗? 我把rewind 加在 gets前面,也不执行gets,这是为什么呢?
pcboyxhy 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yangboazaaza 的回复:]
为什么man rewind 找不到呢?
[/Quote]

因为你没有安装相关的manpages

NAME
fgetpos, fseek, fsetpos, ftell, rewind - reposition a stream

SYNOPSIS
#include <stdio.h>

int fseek(FILE *stream, long offset, int whence);

long ftell(FILE *stream);

void rewind(FILE *stream);

int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, fpos_t *pos);

DESCRIPTION
The fseek() function sets the file position indicator for the stream
pointed to by stream. The new position, measured in bytes, is obtained by
adding offset bytes to the position specified by whence. If whence is set
to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of
the file, the current position indicator, or end-of-file, respectively. A
successful call to the fseek() function clears the end-of-file indicator
for the stream and undoes any effects of the ungetc(3) function on the same
stream.

The ftell() function obtains the current value of the file position indica‐
tor for the stream pointed to by stream.

The rewind() function sets the file position indicator for the stream
pointed to by stream to the beginning of the file. It is equivalent to:

(void) fseek(stream, 0L, SEEK_SET)

except that the error indicator for the stream is also cleared (see clear‐
err(3)).
The fgetpos() and fsetpos() functions are alternate interfaces equivalent
to ftell() and fseek() (with whence set to SEEK_SET), setting and storing
the current value of the file offset into or from the object referenced by
pos. On some non-Unix systems an fpos_t object may be a complex object and
these routines may be the only way to portably reposition a text stream.

RETURN VALUE
The rewind() function returns no value. Upon successful completion, fget‐
pos(), fseek(), fsetpos() return 0, and ftell() returns the current offset.
Otherwise, -1 is returned and errno is set to indicate the error.

ERRORS
EBADF The stream specified is not a seekable stream.

EINVAL The whence argument to fseek() was not SEEK_SET, SEEK_END, or
SEEK_CUR.

The functions fgetpos(), fseek(), fsetpos(), and ftell() may also fail and
set errno for any of the errors specified for the routines fflush(3),
fstat(2), lseek(2), and malloc(3).

CONFORMING TO
C89, C99.

SEE ALSO
lseek(2), fseeko(3)

COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can be
found at http://www.kernel.org/doc/man-pages/.
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
为什么man rewind 找不到呢?
pcboyxhy 2009-10-20
  • 打赏
  • 举报
回复
rewind(stdin); //一般是这么做的,不推荐fflush,它是用于输出流的
flushall不是标准C函数,也不是Linux系统调用
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
这是另外两个函数:
/******** _send************/
int _send()
{


int fd,size;
char fname[1024],buffer[4096];

/*输入要传输的文件*/
printf("请输入您要传输的文件路径:\n");
fflush(NULL);
gets(fname);
/*打开文件*/
fd = open(fname,O_RDONLY,0);
/*读取文件*/
while((size = read(fd,buffer,4096))>0)
{
/*调用client函数,传输文件*/
// client(buffer);
printf("%s",buffer);
}
if(size == -1)
{
fprintf(stderr,"Read Error: %s\a\n",strerror(errno));
exit(-1);
}
/*传输结束,关闭文件*/
close(fd);
printf("传输完毕!\n");
return 1;
}
/********** main ***********/
int main()
{
int ch;
char *buf = NULL;
printf("####################################");
printf("####################################");
printf("######## 欢迎使用我的my_qq软件 #########\n");
printf("####################################");
printf("####################################");
printf("\n\n\n\n\n");

printf("传递文件输入:1\n");
printf("聊 天 输 入 :2\n");
printf("群 聊 输 入 :3\n");
/***************选择模式**************/
scanf("%d",&ch);
fflush(NULL);
/***********调用客户端函数*************/
client(ch);
}
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
我在下次 要 gets(filename);不清理好像不执行这个gets阿
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
我在下次 要 gets(filename);不清理好像不执行这个gets阿
Garfield 2009-10-20
  • 打赏
  • 举报
回复
man不到这个函数。
清不清理有啥区别?
Steven_0610 2009-10-20
  • 打赏
  • 举报
回复
哦,这应该是个清理缓冲区问题。

70,024

社区成员

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

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