C语言出现几个我不太懂的现象
1.我不太明白图片中的阴影部分(图片在下面),有长有短是什么意思(导致我读第一行最后一个字符必须用buf1[strlen(buf1) - 23]) 2.我不太明白我这个代码运行出来,结果是数字后面带个小数点,我不明白小数点是从哪里来的
if (NULL == fp1)
{
printf("open file COM4_2023-01-09_17.33.45.obs");
}
if (NULL == fp2)
{
printf("open file 321.txt");
}
//一般的判断文件打开状态
rewind(fp1); //指针指向文件开头
int i;
char ch, ch1;
char buf1[1024];
int x1 = 0;
int x2 = 0;
while (!feof(fp1))
{
//fgets(buf1, 1024, fp1); // 读取一行字符到buf1字符数组
////printf("buf1=%s\n",buf1);
//// strlen(buf1) 读取的这行字符的长度,
//// 如果读取到此行结束的换行符,则strlen(buf1)-1 为换行符的位置,strlen(buf1)-2为肉眼可见的最后一个字符
//if (buf1[strlen(buf1) - 23] == '5') { // 如果读取的这行字符倒数第一个字符为'5',则复制此行及接下来5行到2.txt
// fprintf(fp2, "%s", buf1); // 将此行写入2.txt
// // 读取接下来5行,写入2.txt
// for (i = 0; i < 5; i++) {
// fgets(buf1, 1024, fp1);
// fprintf(fp2, "%s", buf1);
// }
//}
//else { // 如果不是'5'结尾,则将接下来5行读走
// for (i = 0; i < 5; i++) {
// fgets(buf1, 1024, fp1);
// //fprintf(fp2,"%s",buf1);
// }
//}
fgets(buf1, 1024, fp1);
switch (buf1[strlen(buf1) - 23])
{
case '1':
printf("%c", buf1[strlen(buf1) - 23]);
fgets(buf1, 1024, fp1);
case '2':
printf("%c", buf1[strlen(buf1) - 23]);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
case '3':
printf("%c", buf1[strlen(buf1) - 23]);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
case '4':
printf("%c", buf1[strlen(buf1) - 23]);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
fgets(buf1, 1024, fp1);
case '5':
printf("%c", buf1[strlen(buf1) - 23]);
fprintf(fp2, "%s", buf1);
for (i = 0; i < 5; i++)
{
fgets(buf1, 1024, fp1);
fprintf(fp2, "%s", buf1);
}
default:
printf("%c", buf1[strlen(buf1) - 23]);
}
}
fclose(fp1);
fclose(fp2);
fp1 = NULL;
fp2 = NULL;
printf("复制数据完毕!\n");
return 0;
}
数据是这种模板:
0 2023 01 10 09 17 56.5080000 0 5
A11 24444444 44444422 999999
A13 24444444 44444422 999999
A14 24444444 44444422 999999
A16 24444444 44444422 999999
D23 2843.492 24.000
0 2023 01 10 09 17 56.5080000 0 5
A11 24444444 44444422 999999
A13 24444444 44444422 999999
A14 24444444 44444422 999999
A16 24444444 44444422 999999
D23 2843.492 24.000
0 2023 01 10 09 17 56.5080000 0 5
A11 24444444 44444422 999999
A13 24444444 44444422 999999
A14 24444444 44444422 999999
A16 24444444 44444422 999999
D23 2843.492 24.000
0 2023 01 10 09 17 56.5080000 0 5
A11 24444444 44444422 999999
A13 24444444 44444422 999999
A14 24444444 44444422 999999
A16 24444444 44444422 999999
D23 2843.492 24.000
0 2023 01 10 09 17 56.5080000 0 5
A11 24444444 44444422 999999
A13 24444444 44444422 999999
A14 24444444 44444422 999999
A16 24444444 44444422 999999
D23 2843.492 24.000