1. 在定义变量的时候赋值,最简便。
char file[256] = "D:/data/scope.dat";
2.使用字符串赋值函数
#include <cstring> // 或 #include <string.h>
char file[256];
strcpy(file, "D:/data/scope.dat");
//或者
#include <cstring> // 或 #include <string.h>
char file[256];
strncpy(file, "D:/data/scope.dat", sizeof(file) - 1); // 减去1是为了留出空间给'\0'
file[sizeof(file) - 1] = '\0'; // 确保字符串以'\0'结尾
3.通过索引赋值
char file[256];
file[0] = 'D';
file[1] = ':';
// ...(继续为每个字符赋值)
file[21] = '\0'; // 确保字符串正确结束