并行计算+Linux file操作

用户输入到文件并读取

基于stream

cpp 复制代码
#include <stdio.h>

int main()
{
    // 打开要写入的文件
    FILE *fp = fopen("file.txt", "w");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }

    // 从命令行中读取输入
    char buffer[1024];
    printf("Enter text to write to file: ");
    fgets(buffer, sizeof(buffer), stdin);

    // 将读取到的输入写入文件
    fprintf(fp, "%s", buffer);

    // 关闭文件
    fclose(fp);

    printf("Text written to file successfully\n");

    return 0;
}
c 复制代码
#include <stdio.h>

int main()
{
    // 打开要读取的文件
    FILE *fp = fopen("file.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }

    // 读取文件中的数据
    char buffer[1024];
    int n = fread(buffer, sizeof(char), sizeof(buffer), fp);
    if (n > 0) {
        printf("Read %d bytes from file\n", n);
        printf("File content: %s", buffer);
    } else {
        printf("Failed to read file\n");
    }

    // 关闭文件
    fclose(fp);

    return 0;
}

基于文件标识符

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    int fd = open("file.txt", O_RDWR);
    if (fd == -1) {
        printf("Failed to open file\n");
        return 1;
    }

    // 读取文件中的数据
    char buffer[1024];
    int n = read(fd, buffer, sizeof(buffer));
    if (n > 0) {
        printf("Read %d bytes from file\n", n);
        printf("File content: %s", buffer);
    } else {
        printf("Failed to read file\n");
    }

    // 将数据写入文件
    char data[] = "Hello, world!";
    int m = write(fd, data, sizeof(data));
    if (m > 0) {
        printf("Wrote %d bytes to file\n", m);
    } else {
        printf("Failed to write file\n");
    }
    
    // 从用户输入中读取数据
    char buffer[1024];
    printf("Enter text to write to file: ");
    fgets(buffer, sizeof(buffer), stdin);
    // 将数据写入文件
    int n = write(fd, buffer, sizeof(buffer));
    if (n > 0) {
        printf("Wrote %d bytes to file\n", n);
    } else {
        printf("Failed to write file\n");
    }

    // 定位文件指针
    lseek(fd, 0, SEEK_END);
    long size = ftell(fd);
    printf("File size: %ld bytes\n", size);

    // 关闭文件
    close(fd);

    return 0;
}

按行写入和输出

c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t nread;

    // 打开文件
    fp = fopen("file.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }

    // 逐行读取文件并输出
    while ((nread = getline(&line, &len, fp)) != -1) {
        printf("%s", line);
    }

    // 关闭文件
    fclose(fp);
    if (line) {
        free(line);
    }

    return 0;
}
c 复制代码
#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t nread;

    // 打开文件
    fp = fopen("file.txt", "w");
    if (fp == NULL) {
        printf("Failed to open file\n");
        return 1;
    }

    // 从用户输入中读取数据,并按行写入文件
    printf("Enter text to write to file (type 'quit' to exit):\n");
    while (1) {
        nread = getline(&line, &len, stdin);
        if (nread == -1 || strncmp(line, "quit", 4) == 0) {
            break;
        }
        fprintf(fp, "%s", line);
    }

    // 关闭文件
    fclose(fp);
    if (line) {
        free(line);
    }

    return 0;
}
相关推荐
正小安1 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch2 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光2 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   2 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发
长路 ㅤ   2 小时前
vue-live2d看板娘集成方案设计使用教程
前端·javascript·vue.js·live2d
Fan_web3 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
安冬的码畜日常3 小时前
【CSS in Depth 2 精译_044】第七章 响应式设计概述
前端·css·css3·html5·响应式设计·响应式
韩楚风4 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
莹雨潇潇4 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
陈苏同学4 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm