学习笔记2 代码

静态库测试代码

gcc src/hello.c -c -Iinclude -o bin/hello.o
cd bin
ar rcsv libhello.a hello.o
cd ..
mv bin/libhello.a lib
gcc src/main.c -Iinclude -Llib -lhello -o bin/hello

动态库测试代码

gcc -fPIC -shared src/hello.c -Iinclude -o lib/libhello.so
export LD_LIBRARY_PATH = '/home/study/lib'
gcc src/main.c -Iinclude -Llib -lhello -o bin/h2

文件相关

将文本文件转换为二进制文件:

#include <stdio.h>  

int main() {    
    FILE *inputFile, *outputFile;
    char inputFileName[] = "input.txt"; // 替换为输入文件名
    char outputFileName[] = "output.bin"; // 替换为输出文件名
    char ch;

    // 打开输入文本文件
    inputFile = fopen(inputFileName, "r");
    if (inputFile == NULL) {
        perror("无法打开输入文件");
        return 1;
    }

    // 打开输出二进制文件
    outputFile = fopen(outputFileName, "wb");
    if (outputFile == NULL) {
        perror("无法创建输出文件");
        fclose(inputFile);
        return 1;
    }

    // 逐字符读取文本文件并写入二进制文件
    while ((ch = fgetc(inputFile)) != EOF) {
        fwrite(&ch, sizeof(char), 1, outputFile);
    }

    // 关闭文件
    fclose(inputFile);
    fclose(outputFile);

    printf("文本文件转换为二进制文件成功。\n");
    return 0;
}
posted @ 2023-09-17 16:55  20211423袁艺  阅读(6)  评论(0编辑  收藏  举报