[英]CSV(ish) text handling - incorrect output on Windows, works on Linux
我正在學習 C,但我不明白為什么這段代碼不起作用。
它應該跳過第一個字符,將其分隔為 8,4,4,4,4。 但它會在第二列的開頭添加一個額外的“0”並移動 rest。
此外,當我嘗試在 Windows 上運行它時,我看不到任何結果。 有時候打不開文件,有時候output是錯的。
#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];
output 使用 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 上的整個 output(盡管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
沒有任何必要將不同的字段從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
);
編譯器將“進行數學運算”,將這些總和折疊成 object 文件中的單個值。 在源代碼中以這種方式排列的所有數字清楚地表明了不同的偏移量。
另一件事...如果每行的第一個字符被“跳過”,那么您希望位置 1 到 28 中的字符... s[28] = 0
既錯誤又不必要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.