我正在学习C,我不明白为什么这段代码不能工作。
它应该跳过第一个字符,将其分隔为8,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4。
而且,当我试图在Windows上运行它时,我看不到任何结果。有时它无法打开文件,有时输出是错误的。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const int MAX_LINES = 10000000;
char s[1000];
int lines;
FILE *fptrIn, *fptrOut;
void convertData(char *s) {
s[28] = 0;
char gp1[8 + 1] = {0};
char gp2[4 + 1] = {0};
char gp3[4 + 1] = {0};
char gp4[4 + 1] = {0};
char gp5[4 + 1] = {0};
char gp6[4 + 1] = {0};
strncpy(gp1, s + 1, 8);
strncpy(gp2, s + 8, 4);
strncpy(gp3, s + 12, 4);
strncpy(gp4, s + 16, 4);
strncpy(gp5, s + 20, 4);
strncpy(gp6, s + 24, 4);
fprintf(fptrOut, "%s;%s;%s;%s;%s;%s\n", gp1, gp2, gp3, gp4, gp5, gp6);
}
int main() {
if ((fptrIn = fopen("test.txt", "r")) == NULL) {
printf("Error opening file!");
return 1;
}
fptrOut = fopen("testout1.txt", "w");
fprintf(fptrOut, "Position;Sens1;Sens2;Sens3;Check;Time\n");
while(fgets(s, sizeof s, fptrIn) != NULL) {
lines++;
if (strlen(s) < 28)
continue;
printf("Line %d#:\n", lines);
printf("%s\n", s);
convertData(s);
if (lines == MAX_LINES) {
break;
}
}
fclose(fptrIn);
fclose(fptrOut);
return 0;
}输入数据:
U66ACA1000D8007670000035CBE5Cd;
U66C668000D0A07DA0000037CBF60;
U66DF84000C9908480000038CC05A(;
U66F8A0000C2A08B6000003A9C154Ä;
U67114A800BBB0923000003C9C24E„;
U6729F5000B490991000003D9C348];使用Linux的输出:
Position; Sens1; Sens2; Sens3; Check; Time;
66ACA100; 00D8; 0076; 7000; 0035; CBE5;
66C66800;00D0;A07D;A000;0037;CBF6;
66DF8400;00C9;9084;8000;0038;CC05;
66F8A000;00C2;A08B;6000;003A;9C15;
67114A80;00BB;B092;3000;003C;9C24;
6729F500;00B4;9099;1000;003D;9C34;下面是Windows上的整个输出(尽管WSL正在运行):
Position;Sens1;Sens2;Sens3;Check;Time;
66ACA100;0D80;0767;0000;035C;BE5
00F3B054;8000;0039;9DDE;2‘
U;69F
27000003;A6FD;687
;U6D1;D3B8;000
3731CEEÕ;
U70;4A17;0002;3901;7A0
U73764;8000;3F20;F570;0000;340发布于 2022-07-27 08:41:13
不需要将不同的字段从s[ ]中转移出去.
printf()会做你想做的..。
printf( "%8.8s,%4.4s,%4.4s,%4.4s,%4.4s,%4.4s\n",
s + 1,
s + 1+8,
s + 1+8+4,
s + 1+8+4+4,
s + 1+8+4+4+4,
s + 1+8+4+4+4+4
);编译器将“做数学计算”,将这些和折叠成对象文件中的单个值。在源代码中以这种方式排列的所有数字清楚地表明了不同的偏移量。
另一件事..。如果每一行的第一个字符是“跳过”,那么您希望在位置1到28的字符.s[28] = 0是错误的,也是不必要的。
https://stackoverflow.com/questions/73134296
复制相似问题