c语言中文件读入处理写入实战

可以使用文件操作和字符串处理函数来实现将读取的文件内容去掉空白的内容,然后将其连起来的功能。下面是一个示例代码:

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

void removeWhitespace(char* str) {
    int i, j;
    for (i = 0, j = 0; str[i] != '\0'; i++) {
        if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
            str[j++] = str[i];
        }
    }
    str[j] = '\0';
}

int main() {
    FILE* file = fopen("input.txt", "r");
    if (file == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    fseek(file, 0, SEEK_END);
    long fileSize = ftell(file);
    fseek(file, 0, SEEK_SET);

    char* content = (char*)malloc(fileSize + 1);
    if (content == NULL) {
        printf("Failed to allocate memory.\n");
        fclose(file);
        return 1;
    }

相关推荐

  1. c语言文件读入处理写入实战

    2024-01-16 13:44:05       54 阅读
  2. Python,如何读取写入文件

    2024-01-16 13:44:05       42 阅读
  3. C语言】如何将数据写入文件

    2024-01-16 13:44:05       45 阅读
  4. C语言】如何从文件读取数据?

    2024-01-16 13:44:05       41 阅读
  5. Python文件读取写入

    2024-01-16 13:44:05       29 阅读
  6. C++ 实现以xml的格式写入文件

    2024-01-16 13:44:05       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-16 13:44:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-16 13:44:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-16 13:44:05       82 阅读
  4. Python语言-面向对象

    2024-01-16 13:44:05       91 阅读

热门阅读

  1. 2.1 数组

    2024-01-16 13:44:05       55 阅读
  2. linux nginx配置链接访问图片

    2024-01-16 13:44:05       48 阅读
  3. Redis-集群

    2024-01-16 13:44:05       64 阅读
  4. leetcode30天pandas挑战day1笔记

    2024-01-16 13:44:05       55 阅读
  5. 思科无线AP 2802无法注册,手工指定控制器的IP

    2024-01-16 13:44:05       41 阅读