RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题

问题[файловый-ввод-вывод]

Martin Hope
Dandelf
Asked: 2024-04-27 18:54:40 +0000 UTC

如何依次打开不同名称的文件?

  • 5

有一个文件夹,其中有大量不同名称的.txt文件,名称的第一部分包含日期,第二部分可以认为是随机的。如何在不知道文件确切名称的情况下按顺序打开一个文件进行读取,直到文件夹末尾?也许您可以从 Windows 获取有关该文件类型的信息,并以某种方式获取其名称并将其传输到程序?

файловый-ввод-вывод
  • 1 个回答
  • 40 Views
Martin Hope
Timon55
Asked: 2022-07-23 14:55:09 +0000 UTC

编码变体 C 数字 ? 输出到二进制文件

  • 0

我正在尝试从这里执行任务 1 ,编码、解码和生成算法是从那里获取的。我无法弄清楚 encode_variant 是如何工作的。为什么它返回 size_t ?好吧,我试着戳一下输入,我给了数字 122,比如说,返回 1,我给 222,返回 2。不知何故,它看起来不太像一个编码数字。下一个问题是如何使用解码从这个编码的数字中获取原始数字。我不明白数字 2 应该以什么形式传输,例如,为了将其解码回 222。任务是生成两个具有 100000000 个随机数的文件,一个应该包含没有编码的数字,另一个用它你需要比较文件大小,我得到相同大小的文件 3907 KB,从逻辑上讲,编码后,这些数字应该占用更小的体积。这是代码

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#define ERROR_FILE_OPEN -3

size_t encode_varint(uint32_t value, uint8_t* buf)
{
    assert(buf != NULL);
    uint8_t* cur = buf;
    while (value >= 0x80) {
        const uint8_t byte = (value & 0x7f) | 0x80;
        *cur = byte;
        value >>= 7;
        ++cur;
    }
    *cur = value;
    ++cur;
    return cur - buf;
}

uint32_t decode_varint(const uint8_t** bufp)
{
    const uint8_t* cur = *bufp;
    uint8_t byte = *cur++;
    uint32_t value = byte & 0x7f;
    size_t shift = 7;
    while (byte >= 0x80) {
        byte = *cur++;
        value += (byte & 0x7f) << shift;
        shift += 7;
    }
    *bufp = cur;
    return value;
}

/*
 * Диапазон             Вероятность
 * -------------------- -----------
 * [0; 128)             90%
 * [128; 16384)         5%
 * [16384; 2097152)     4%
 * [2097152; 268435455) 1%
 */
uint32_t generate_number()
{
    const int r = rand();
    const int p = r % 100;
    if (p < 90) {
        return r % 128;
    }
    if (p < 95) {
        return r % 16384;
    }
    if (p < 99) {
        return r % 2097152;
    }
    return r % 268435455;
}

int main(void){
    uint8_t* mybuffer;
    mybuffer = malloc(sizeof(uint8_t));
    FILE *output1 = NULL;
    FILE *output2 = NULL;
    output1 = fopen("C:/Users/forjo/Documents/C Prog/Programms/bitwise/compressed.dat","wb");
    if (output1 == NULL) {
        printf("Error opening file");
        getch();
        exit(ERROR_FILE_OPEN);
    }
    output2 = fopen("C:/Users/forjo/Documents/C Prog/Programms/bitwise/uncompressed.dat","wb");
    if (output1 == NULL) {
        printf("Error opening file");
        getch();
        exit(ERROR_FILE_OPEN);
    }
    size_t count = 1;
    for (int i=0; i<1000000; i++){
    uint32_t random = generate_number();
    uint32_t compress = encode_varint(random,mybuffer);
    fwrite(&compress, sizeof(compress), count, output1);
    fwrite(&random, sizeof(random), count, output2);
    }
    return 0;
}
c файловый-ввод-вывод
  • 1 个回答
  • 52 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5