c++ stdin 二进制

怎么将stdin里的二进制文件信息读入到char*
tar -c .[!.] | lz4 -1 | test

test.cpp

            #define buf_size 1048576
            char buf[buf_size] = { 0 };
            fread(buf, sizeof(char), sizeof(buf), stdin);

用上面的方法读取有问题

tar -c .[!.] | lz4 -1 | 生成二进制文件信息,有什么方法可以从通道内获取到这部分信息


#include <stdio.h>
int main() {
 int ch;
 while (1) {
  ch=fgetc(stdin);
  if (ch==EOF) break;//如果无字符可读,退出程序
  printf("%02X ",(unsigned char)ch);//将刚读出的字节用大写两位十六进制字符后跟一个空格显示出来
 }
 return 0;
}

秒杀以下AIGC或chatGPT! (^_^)

char *buffer;
size_t size;

// 获取 stdin 中的大小
fseek(stdin, 0, SEEK_END);
size = ftell(stdin);
rewind(stdin);

// 分配内存
buffer = (char*) malloc(size);

// 读取文件
fread(buffer, size, 1, stdin);

要从标准输入(stdin)中读取二进制文件信息并存储到char*中,可以使用以下方法:

#include <iostream>
#include <vector>

int main() {
    // 读取二进制文件信息
    std::vector<char> buffer(std::istreambuf_iterator<char>(std::cin), {});

    // 将数据存储到 char* 中
    char* data = buffer.data();
    size_t size = buffer.size();

    // 输出读取的数据
    for (size_t i = 0; i < size; i++) {
        std::cout << data[i];
    }

    return 0;
}

这段代码使用std::istreambuf_iteratorstd::cin(标准输入)读取二进制文件的内容,并将其存储在std::vector<char>类型的缓冲区中。然后,通过调用data()方法获取char*指针和size()方法获取数据的大小。你可以根据需要使用这些数据进行后续操作。

请注意,在读取二进制数据时,确保输入的数据流不会被解释为文本数据进行编码转换。在某些操作系统中,可能需要以二进制模式打开流来避免默认的文本转换。

你好,你的问题初步尝试如下


#define buf_size 1048576
char *buf = new char[buf_size]; // 动态分配内存空间
size_t ret = fread(buf, sizeof(char), buf_size, stdin); // 从标准输入读取数据,并检查返回值
if (ret == 0) {
    if (feof(stdin)) {
        printf("End of input\n"); 
    } else if (ferror(stdin)) {
        perror("fread");
        exit(1); 
    }
} else {
    printf("Read %zu bytes\n", ret); // 打印读取的字节数
}
// 对buf进行处理
delete[] buf; // 释放内存空间

要解决这个问题,我们需要使用C++的输入/输出流,并使用stdin和stdout来读取和写入二进制数据。

首先,我们需要定义一个时间变量,它应该存储当前时间t的秒数。我们可以使用系统调用(system call)来获取当前时间。

#include <iostream>
#include <ctime>

using namespace std;

int main() {
    // 获取当前时间
    struct tm now;
    time_t now_ctime;
    tm *now = localtime(&now);
    now_ctime = ctime(&now);

    // 获取当前时间对应的秒数
    long long t_ctime = now_ctime - tm_mon + now_ctime * 86400 + now_ctime * 3600;

    // 将秒数转换为分钟
    long long t_mtime = t_ctime * 60;

    // 将分钟转换为小时
    long long t_htime = t_mtime * 60 * 60;

    // 将小时转换为分钟
    long long t_mtime_h = t_htime * 60;

    // 将分钟转换为小时
    long long t_htime_m = t_mtime_h * 60;

    // 将小时转换为分钟
    long long t_mtime_h_m = t_htime_m * 60;

    // 存储当前时间t_ctime、小时t_htime_m、分钟t_mtime_h和秒数t_htime_m
    cout << "t_ctime = " << t_ctime << endl;
    cout << "t_htime_m = " << t_htime_m << endl;
    cout << "t_mtime_h = " << t_mtime_h << endl;
    cout << "t_mtime_h_m = " << t_mtime_h_m << endl;

    // 输出阶跃函数
    cout << "阶跃函数: " << endl;
    for (int i = 0; i < 1000; i++) {
        int a = t_htime_m - (t_htime_m_h * 60 + t_htime_m_m * 60 + t_htime_m_h * 60 + t_htime_m_m * 30);
        int b = t_ctime - t_htime_m - a * 60;
        if (b < 0) {
            break;
        }
        cout << a << " ";
        t_htime_m += b;
        t_ctime += t_htime_m * 86400;
    }

    return 0;
}

在这段代码中,我们定义了一个时间变量t_ctime、t_htime_m、t_mtime_h和t_mtime_m_h,它们分别存储当前时间t、小时t_h和分钟t_m的秒数。

我们使用系统调用获取当前时间,并将结果存储在t_ctime、t_htime_m、t_mtime_h和t_mtime_m


#include <io.h>
#include <fcntl.h>
 
int main()
{
    _setmode(_fileno(stdin), _O_BINARY);

以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:

要从标准输入读取二进制数据到char数组,可以使用以下方法:

  1. 不要使用fread()直接读取stdin,因为stdin默认是文本模式,会对二进制数据进行转换,导致读取出错。

  2. 要读取二进制数据,需要将stdin设置为二进制模式。可以使用freopen()重新打开stdin:


cpp
freopen(NULL, "rb", stdin);  // 重新打开stdin,设置为二进制读模式

  1. 然后使用fread()读取数据:
cpp
size_t n;
while ((n = fread(buf, 1, buf_size, stdin)) > 0) {
    // 处理读取到的buf数据
}

  1. fread()返回实际读取的字节数,需要在循环中重复调用以读取全部数据。

  2. 读取完数据后,要关闭stdin。

综上,完整的代码示例如下:

cpp
#define buf_size 1048576  
char buf[buf_size] = { 0 };  

// 设置stdin为二进制读模式  
freopen(NULL, "rb", stdin);  

// 循环读取stdin数据 
size_t n;  
while ((n = fread(buf, 1, buf_size, stdin)) > 0) {  
    // 处理buf数据
}

// 关闭stdin
fclose(stdin); 

按照这个方法,就可以正确读取stdin中的二进制数据流,并存储到char数组中。