相关疑难解决方法(0)

C中的read()和fgets()之间的区别

我想从stdin流中读取.使用read()或fgets()从stdin流中读取是否有任何区别.

我用fgets附加以下两段代码并读取.使用fgets,我可以使用java程序轻松地编写和读取c程序.随着读写,我的java程序挂起等待C程序的输出,而不是来.

我只是读了一行保持它在buf并附加A到它.

Java程序能够与以下程序通信,该程序与fgets和puts一起使用.

#include <stdio.h>
#include <string.h>
#define SIZE  200000
main()
{
int rc;
int df;
int i;
char buf[SIZE];
for(i=0;i<=120000;i++) {
      memset(buf,'\0',SIZE);
      if(!fgets(buf,SIZE-1,stdin))
        continue;
      strcat(buf,"A_A_A_A_A_A_A");
      puts(buf);
}
Run Code Online (Sandbox Code Playgroud)

}

但没有read()和write()

main()
{
int rc;
int df;
int i;
char buf[32768];
rc = fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
//rc = fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
FILE *fp;
for (;;) 
{
    int rc=-1;
    memset(buf,'\0',32768);
    //rc = fread(buf,5, 1, stdin);
    rc = read(fileno(stdin),buf,32768); 
    if (rc > 0)
    {
        strcat(buf,"B_B_B_B_B_B_B_B_B");
        write(fileno(stdout),buf,strlen(buf));

    }
}
Run Code Online (Sandbox Code Playgroud)

}

有人可以说出原因.我仍然觉得很难弄明白

c fgets

9
推荐指数
2
解决办法
1万
查看次数

fgets和fread之间的区别

我有以下代码:

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

int main(void)
{
int lendata;
printf("Content-type:text/html\n\n");
printf("<html><body>");
lendata = atoi(getenv("CONTENT_LENGTH"));
char *buf = malloc(lendata+1);
fread(buf,lendata,1,stdin);
printf("%s\n<br>",buf); 
printf("%d",lendata);   
free(buf);
printf("</body></html>");
return 0;

}
Run Code Online (Sandbox Code Playgroud)

当我使用fgets时,它会截断显示的数据.但是当我使用fread时,它会显示所有内容.顺便说一句,这是使用post方法上传html文件的cgi脚本.任何帮助都会受到极大的抨击.

c post cgi

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×2

cgi ×1

fgets ×1

post ×1