fread()在在线编辑器和本地机器(C++)上的不同行为
Different behaviour of fread() on online editor and the local machine (C++)
当我在C++代码中使用fread()
并在在线编辑器上编译它(使用gcc)时,它工作得很好。但是,当我在本地机器上运行相同的代码时,它会无限等待,除非我在windows PC上点击ctrl+z。为什么会这样?
我正在读取代码中的stdin(如果这有帮助的话)。
这是代码(但不是我的。我只是用它来理解它)。
#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
#define size 65536
int main()
{
char b[size];
int t=0,n,k,cnt=0;
int c,j;
scanf("%d %dn",&n,&k);
printf("%d %dn", n, k);
while((c = fread(b,1,size,stdin))>0)
{
printf("%dn",c);
for(j=0;j<c;j++)
{
if(b[j]=='n')
{
if(t%k==0)cnt++;
t = 0;
}
else
{
t = (t*10) + (b[j]-'0');
}
}
}
printf("%dn",cnt);
return 0;
}
它一直读到输入流的末尾。
在线编辑器通过管道传输来自文件(或类似文件)的输入流,并在所提供的输入的末尾结束。
您的本地机器从控制台读取它(除非您将某些内容通过管道传输到程序中),直到您使用Ctrl-Z关闭输入流,它才会结束。
相关文章:
- 在线编译器中的分段C++没有打印消息
- 在线程之间拆分任务总是值得的吗?
- join() 失败,如果在线程内部调用 io_context.run()
- 如何在C++中在线组装?
- 在线程中读取无符号整数时,c++ 位是否以原子方式切换?
- 在线程函数中处理数据向量时进行线程竞速
- 我应该在线程上调用 processEvents() 吗?
- 如何创建始终在线的过程?
- pthread只有在线程数量较少时才可以正常工作
- boost::asio::io_service 在线程中,不会在应用程序退出时退出
- 如何在线程中初始化对象,然后在其他地方使用它?
- 在线程 A 中创建一个 std::thread 对象,在线程 B 中连接
- C++:我可以在线程仍在运行时为线程提供新值,还是必须先结束它?
- 为什么我的程序无法在GDB在线编译器/调试器或Visual Studio C++ 2019中运行
- 亚马逊在线评估编码问题,以找到第n个几何级数
- C++在线程中将多个对象传递给一个函数
- 在线程C++中创建一个套接字
- 有没有一种方法可以从本地C++程序写入在线CMS中的文件
- 我们如何在线程中使用参数
- 在 "CodePad" 中执行链表操作时转储的核心(这是一个在线C++编译器)