1 逗号运算符
运算规则:左边执行完毕,才会执行右边的表达式;
最后一个表达式的值作为整个表达式的结果;
int aa = (10, 20, 30); // aa = 30
int a = 10;
int b = 20;
int c = 30;
c = (a = b + 10, b = a + 10); // a = 30, b = 40, c = 40
2 枚举
enum Week
{
// start 0
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun
};
Week wk = Wed;
printf("wk = %d", wk);
3 文件
3.1 读写函数
3.2 字符文件读写
read
void read_demo()
{
const char *fileName = "E:/source/demo.txt";
// [1] open : file1 指针指向一块内存空间,空间由系统去申请的
FILE *file1 = fopen(fileName, "r");
if (file1 == NULL)
{
printf("open filed!\n");
return;
}
// [2] 读取文件内容
#if 0
char ch = fgetc(file1);
printf("%c", ch);
#else
char ch = 0;
while ((ch = fgetc(file1)) != EOF) // EOF=-1 结尾标志
{
printf("%c", ch);
}
#endif
// [3] close :
fclose(file1);
file1 = NULL;
}
void read_demo_contents()
{
const char *fileName = "E:/source/demo.txt";
// [1] open : file1 指针指向一块内存空间,空间由系统去申请的
FILE *file1 = fopen(fileName, "r");
if (file1 == NULL)
{
printf("open filed!\n");
return;
}
// [2] 读取文件内容
char contents[128] = {0};
int idx = 0;
while ((contents[idx] = fgetc(file1)) != EOF) // EOF=-1 结尾标志
{
idx++;
}
// printf
printf("contents = %s", contents);
// [3] close :
fclose(file1);
file1 = NULL;
}
write
void write_demo()
{
// [1] open
FILE *writer = fopen("E:/source/write.txt", "w");
if (writer == NULL)
{
printf("open failed !\n");
return;
}
// [2] write
char *ch = "Hello xhh";
for (int i = 0; i < strlen(ch); ++i)
{
fputc(ch[i], writer);
}
// [3] close
fclose(writer);
writer = NULL;
printf("writer SUCCESS !\n");
}