[toc]

使用C语言读取与写入文件

1. c语言读取文件

  1. 新建一个文本文件,命名为first.txt。

  2. 在first.txt中编辑文本。

  3. 编写C语言代码在程序中读取文本内容。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>

    int main()
    {
    FILE* fp = NULL;
    char buff[255];

    fp = fopen("D:\\Users\\3\\Desktop\\first.txt", "r");
    fgets(buff, 255, (FILE*)fp);
    printf("1: %s\n", buff);
    fgets(buff, 255, (FILE*)fp);
    printf("2: %s\n", buff);
    fgets(buff, 255, (FILE*)fp);
    printf("3: %s\n", buff);
    fclose(fp);
    return 0;
    }
  4. 编译运行

2.C语言写入给文本文件中写入字符串

  1. 编写代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main()
{
FILE* fp = NULL;

fp = fopen("D:\\Users\\3\\Desktop\\first.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
  1. 打开first.txt查看文件发现文本内容更改
1
2
This is testing for fprintf...
This is testing for fputs...

3.C语言以二进制的方式读取文本

  1. 编写C语言代码,以二进制的方式读取文本。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>

    int main() {
    FILE* file;
    unsigned char buffer[100]; // 用于存储读取的数据
    size_t bytesRead;

    // 以二进制模式打开文件
    file = fopen("D:\\Users\\3\\Desktop\\first.txt", "rb");
    if (file == NULL) {
    perror("无法打开文件");
    return 1;
    }

    // 读取文件内容
    bytesRead = fread(buffer, sizeof(unsigned char), sizeof(buffer), file);
    if (bytesRead > 0) {
    // 处理读取的数据
    printf("读取了 %zu 字节\n", bytesRead);
    }

    // 关闭文件
    fclose(file);
    return 0;
    }

4.C语言以二进制的方式读取文本并打印字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
FILE* file;
unsigned char buffer[100]; // 用于存储读取的数据
size_t bytesRead;

// 以二进制模式打开文件
file = fopen("D:\\Users\\3\\Desktop\\first.txt", "rb");
if (file == NULL) {
perror("无法打开文件");
return 1;
}

// 读取文件内容
bytesRead = fread(buffer, sizeof(unsigned char), sizeof(buffer), file);
if (bytesRead > 0) {
// 以字符形式打印读取的数据
printf("读取了 %zu 字节:\n", bytesRead);
for (size_t i = 0; i < bytesRead; i++)
{
printf("%c", buffer[i]);

}
printf("\n");
}

// 关闭文件
fclose(file);
return 0;
}

5.C语言以二进制的方式读取文本并打印16进制数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
FILE* file;
unsigned char buffer[100]; // 用于存储读取的数据
size_t bytesRead;
// 以二进制模式打开文件
file = fopen("D:\\Users\\3\\Desktop\\first.txt", "rb");
if (file == NULL) {
perror("无法打开文件");
return 1;
}
// 读取文件内容
bytesRead = fread(buffer, sizeof(unsigned char), sizeof(buffer), file);
if (bytesRead > 0) {
// 以十六进制格式打印读取的数据
printf("读取了 %zu 字节:\n", bytesRead);
for (size_t i = 0; i < bytesRead; i++) {
printf("0x%02X ", buffer[i]); // 以两位十六进制格式打印
}
printf("\n");
}
// 关闭文件
fclose(file);
return 0;
}

6. C语言以二进制的方式写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
FILE* file;
unsigned char data[] = { 0x65, 0x66, 0x67, 0x68, 0x69 }; // 要写入的数据
size_t bytesWritten;

// 以二进制模式打开文件(写入模式)
file = fopen("D:\\Users\\3\\Desktop\\first.txt", "wb");
if (file == NULL) {
perror("无法打开文件");
return 1;
}

// 写入数据到文件
bytesWritten = fwrite(data, sizeof(unsigned char), sizeof(data), file);
if (bytesWritten != sizeof(data)) {
perror("写入文件时出错");
}
else {
printf("成功写入 %zu 字节\n", bytesWritten);
}

// 关闭文件
fclose(file);
return 0;
}


使用C语言在windows读取与写入文件

1. windows读取文件

  1. 新建一个文本文件,命名为first.txt。

  2. 在first.txt中编辑文本。

    1
    2
    3
    1.windows read file
    2. cotine
    3. 6666666666666
  3. C语言使用win32api在程序中读取文本内容。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #include <windows.h>
    #include <stdio.h>

    int main()
    {
    HANDLE hFile;
    DWORD bytesRead;
    char buffer[100];

    // 打开文件
    hFile = CreateFileA("D:\\Users\\3\\Desktop\\first.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
    printf("无法打开文件,错误代码: %lu\n", GetLastError());
    return 1;
    }

    // 读取文件内容
    if (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
    buffer[bytesRead] = '\0'; // 添加字符串结束符
    printf("读取的内容:\n%s", buffer);
    }
    else {
    printf("读取文件时出错,错误代码: %lu\n", GetLastError());
    }

    // 关闭文件
    CloseHandle(hFile);
    return 0;
    }

  4. 编译运行

2.windows给文本文件中写入字符串

  1. 编写代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hFile;
const char* data = "Hello, World!\n"; // 要写入的字符串
DWORD bytesWritten;

// 以写入模式打开文件(如果文件不存在则创建)
hFile = CreateFileA("D:\\Users\\3\\Desktop\\first.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("无法打开文件,错误代码: %lu\n", GetLastError());
return 1;
}

// 写入字符串到文件
if (WriteFile(hFile, data, strlen(data), &bytesWritten, NULL)) {
printf("成功写入 %lu 字节\n", bytesWritten);
}
else {
printf("写入文件时出错,错误代码: %lu\n", GetLastError());
}

// 关闭文件
CloseHandle(hFile);
return 0;
}

  1. 打开first.txt查看文件发现文本内容更改
1
Hello, World!

3. windows以二进制的方式读取文本并打印字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hFile;
DWORD bytesRead;
char buffer[100]; // 用于存储读取的数据

// 以二进制模式打开文件
hFile = CreateFileA("D:\\Users\\3\\Desktop\\first.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("无法打开文件,错误代码: %lu\n", GetLastError());
return 1;
}

// 读取文件内容
if (ReadFile(hFile, buffer, sizeof(buffer) - 1, &bytesRead, NULL)) {
buffer[bytesRead] = '\0'; // 添加字符串结束符
printf("读取的内容:\n%s", buffer); // 以字符串形式打印
}
else {
printf("读取文件时出错,错误代码: %lu\n", GetLastError());
}

// 关闭文件
CloseHandle(hFile);
return 0;
}

4. windows以二进制的方式读取文本并打印16进制数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hFile;
DWORD bytesRead;
char buffer[100]; // 用于存储读取的数据

// 以二进制模式打开文件
hFile = CreateFileA("D:\\Users\\3\\Desktop\\first.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("无法打开文件,错误代码: %lu\n", GetLastError());
return 1;
}

// 读取文件内容
if (ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL)) {
printf("成功读取 %lu 字节:\n", bytesRead);
// 打印读取的内容(以十六进制格式)
for (DWORD i = 0; i < bytesRead; i++) {
printf("0x%02X ", (unsigned char)buffer[i]);
}
printf("\n");
}
else {
printf("读取文件时出错,错误代码: %lu\n", GetLastError());
}

// 关闭文件
CloseHandle(hFile);
return 0;
}

6. windows以二进制的方式写入文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <windows.h>
#include <stdio.h>

int main() {
HANDLE hFile;
char data[] = { 0x65, 0x66, 0x67, 0x68, 0x69, 0x70 }; // 要写入的字符数组
DWORD bytesWritten;

// 以二进制模式打开文件(如果文件不存在则创建)
hFile = CreateFileA("D:\\Users\\3\\Desktop\\first.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("无法打开文件,错误代码: %lu\n", GetLastError());
return 1;
}

// 写入数据到文件
if (WriteFile(hFile, data, sizeof(data), &bytesWritten, NULL)) {
printf("成功写入 %lu 字节\n", bytesWritten);
}
else {
printf("写入文件时出错,错误代码: %lu\n", GetLastError());
}

// 关闭文件
CloseHandle(hFile);
return 0;
}

使用C语言在windows系统中将Dll文件打包到exe程序中

1.使用WinHex将二进制文件复制

在WinHex选中编辑->复制选块->c源码

2.将二进制代码粘贴到c语言源码中

3.编写c语言代码写入文件

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//main.c
#include <windows.h>
#include <stdio.h>
#include "DllFile.h"

#define DRIVER_FILE_NAME L"dll1.dll"
#define DRIVER_FILE_PATH L"D:\\Users\\3\\Desktop\\dll1.dll"
DWORD WriteDriverFile(WCHAR* DriverFilePath)
{
HANDLE hFile = CreateFileW(DriverFilePath, GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == NULL) { return -1; }
DWORD dwRet = 0;
if (WriteFile(hFile, g_DllFile, sizeof(g_DllFile), &dwRet, NULL) == FALSE)
{
CloseHandle(hFile);
return -2;
}
CloseHandle(hFile);
return TRUE;
}
int main(int argc, char** argv)
{
if (WriteDriverFile(DRIVER_FILE_PATH) < 0)
{
printf("写入失败 错误码:", GetLastError());
}
return 0;
}

二进制文件内容概括

1
2
//DllBuffer.h
unsigned char g_DllFile[111768] ={0x4D,................................};