scottlcy
一般會員
發表:6 回覆:0 積分:1 註冊:2003-10-17
發送簡訊給我
|
各位前輩,我要如何才能使這個while一直讀,每次32個字,然後把全部的值寫回去.
我的資料是 0x010x230x450x670x890xab0xcd0xef0x010x230x450x670x890xab0xcd0xef
#include
#include
#include
#include int main(void)
{
FILE *charFile;
FILE *newCharFile; char in[100];
char input[100];
char temp[100];
char binTable[5];
char out[64]; int index;
int idx;
unsigned char data;
unsigned char data1;
unsigned char data2;
unsigned char data3;
unsigned char data4;
unsigned char data5;
unsigned char data6;
unsigned char data7;
char dataTemp;
char dataTemp1;
char dataTemp2;
char dataTemp3;
char dataTemp4;
char dataTemp5;
char dataTemp6;
char dataTemp7;
char dataTemp8;
char dataTemp9;
char dataTemp10;
char dataTemp11;
char dataTemp12;
char dataTemp13;
char dataTemp14;
char dataTemp15;
char dataOut;
char dataOut1;
char dataOut2;
char dataOut3;
char dataOut4;
char dataOut5;
char dataOut6;
char dataOut7;
char dataOut8;
char dataOut9;
char dataOut10;
char dataOut11;
char dataOut12;
char dataOut13;
char dataOut14;
char dataOut15; charFile = fopen("TEST.c", "rt");
newCharFile = fopen("REPORT.c", "wt"); while(fgets(input, 33, charFile) != NULL)
{
for(index = 0; index < 32; index )
{
if((input[index] == '0') && (input[index 1] == 'x'))
{
temp[0] = input[index 2];
temp[1] = input[index 3];
temp[2] = input[index 6];
temp[3] = input[index 7];
temp[4] = input[index 10];
temp[5] = input[index 11];
temp[6] = input[index 14];
temp[7] = input[index 15];
temp[8] = input[index 18];
temp[9] = input[index 19];
temp[10] = input[index 22];
temp[11] = input[index 23];
temp[12] = input[index 26];
temp[13] = input[index 27];
temp[14] = input[index 30];
temp[15] = input[index 31];
temp[16] = '\0';
break;
}
}
} for(idx = 0; idx < 16; idx )
{
switch(temp[idx])
{
case '0':
binTable[0] = '0';
binTable[1] = '0';
binTable[2] = '0';
binTable[3] = '0';
binTable[4] = '\0';
break; case '1':
binTable[0] = '0';
binTable[1] = '0';
binTable[2] = '0';
binTable[3] = '1';
binTable[4] = '\0';
break; case '2':
binTable[0] = '0';
binTable[1] = '0';
binTable[2] = '1';
binTable[3] = '0';
binTable[4] = '\0';
break; case '3':
binTable[0] = '0';
binTable[1] = '0';
binTable[2] = '1';
binTable[3] = '1';
binTable[4] = '\0';
break; case '4':
binTable[0] = '0';
binTable[1] = '1';
binTable[2] = '0';
binTable[3] = '0';
binTable[4] = '\0';
break; case '5':
binTable[0] = '0';
binTable[1] = '1';
binTable[2] = '0';
binTable[3] = '1';
binTable[4] = '\0';
break; case '6':
binTable[0] = '0';
binTable[1] = '1';
binTable[2] = '1';
binTable[3] = '0';
binTable[4] = '\0';
break; case '7':
binTable[0] = '0';
binTable[1] = '1';
binTable[2] = '1';
binTable[3] = '1';
binTable[4] = '\0';
break; case '8':
binTable[0] = '1';
binTable[1] = '0';
binTable[2] = '0';
binTable[3] = '0';
binTable[4] = '\0';
break; case '9':
binTable[0] = '1';
binTable[1] = '0';
binTable[2] = '0';
binTable[3] = '1';
binTable[4] = '\0';
break; case 'a':
case 'A':
binTable[0] = '1';
binTable[1] = '0';
binTable[2] = '1';
binTable[3] = '0';
binTable[4] = '\0';
break; case 'b':
case 'B':
binTable[0] = '1';
binTable[1] = '0';
binTable[2] = '1';
binTable[3] = '1';
binTable[4] = '\0';
break; case 'c':
case 'C':
binTable[0] = '1';
binTable[1] = '1';
binTable[2] = '0';
binTable[3] = '0';
binTable[4] = '\0';
break; case 'd':
case 'D':
binTable[0] = '1';
binTable[1] = '1';
binTable[2] = '0';
binTable[3] = '1';
binTable[4] = '\0';
break; case 'e':
case 'E':
binTable[0] = '1';
binTable[1] = '1';
binTable[2] = '1';
binTable[3] = '0';
binTable[4] = '\0';
break; case 'f':
case 'F':
binTable[0] = '1';
binTable[1] = '1';
binTable[2] = '1';
binTable[3] = '1';
binTable[4] = '\0';
break;
} if(idx == 0)
{
out[7] = binTable[0];
out[15] = binTable[1];
out[23] = binTable[2];
out[31] = binTable[3];
} if(idx == 1)
{
out[39] = binTable[0];
out[47] = binTable[1];
out[55] = binTable[2];
out[63] = binTable[3];
} if(idx == 2)
{
out[6] = binTable[0];
out[14] = binTable[1];
out[22] = binTable[2];
out[30] = binTable[3];
} if(idx == 3)
{
out[38] = binTable[0];
out[46] = binTable[1];
out[54] = binTable[2];
out[62] = binTable[3];
} if(idx == 4)
{
out[5] = binTable[0];
out[13] = binTable[1];
out[21] = binTable[2];
out[29] = binTable[3];
} if(idx == 5)
{
out[37] = binTable[0];
out[45] = binTable[1];
out[53] = binTable[2];
out[61] = binTable[3];
} if(idx == 6)
{
out[4] = binTable[0];
out[12] = binTable[1];
out[20] = binTable[2];
out[28] = binTable[3];
} if(idx == 7)
{
out[36] = binTable[0];
out[44] = binTable[1];
out[52] = binTable[2];
out[60] = binTable[3];
} if(idx == 8)
{
out[3] = binTable[0];
out[11] = binTable[1];
out[19] = binTable[2];
out[27] = binTable[3];
} if(idx == 9)
{
out[35] = binTable[0];
out[43] = binTable[1];
out[51] = binTable[2];
out[59] = binTable[3];
} if(idx == 10)
{
out[2] = binTable[0];
out[10] = binTable[1];
out[18] = binTable[2];
out[26] = binTable[3];
} if(idx == 11)
{
out[34] = binTable[0];
out[42] = binTable[1];
out[50] = binTable[2];
out[58] = binTable[3];
} if(idx == 12)
{
out[1] = binTable[0];
out[9] = binTable[1];
out[17] = binTable[2];
out[25] = binTable[3];
} if(idx == 13)
{
out[33] = binTable[0];
out[41] = binTable[1];
out[49] = binTable[2];
out[57] = binTable[3];
} if(idx == 14)
{
out[0] = binTable[0];
out[8] = binTable[1];
out[16] = binTable[2];
out[24] = binTable[3];
} if(idx == 15)
{
out[32] = binTable[0];
out[40] = binTable[1];
out[48] = binTable[2];
out[56] = binTable[3];
} cout << binTable << endl;
} data = out[0] - '0';
data = (data << 1) ( out[1] - '0' );
data = (data << 1) ( out[2] - '0' );
data = (data << 1) ( out[3] - '0' );
data = (data << 1) ( out[4] - '0' );
data = (data << 1) ( out[5] - '0' );
data = (data << 1) ( out[6] - '0' );
data = (data << 1) ( out[7] - '0' ); data1 = out[8] - '0';
data1 = (data1 << 1) ( out[9] - '0' );
data1 = (data1 << 1) ( out[10] - '0' );
data1 = (data1 << 1) ( out[11] - '0' );
data1 = (data1 << 1) ( out[12] - '0' );
data1 = (data1 << 1) ( out[13] - '0' );
data1 = (data1 << 1) ( out[14] - '0' );
data1 = (data1 << 1) ( out[15] - '0' ); data2 = out[16] - '0';
data2 = (data2 << 1) ( out[17] - '0' );
data2 = (data2 << 1) ( out[18] - '0' );
data2 = (data2 << 1) ( out[19] - '0' );
data2 = (data2 << 1) ( out[20] - '0' );
data2 = (data2 << 1) ( out[21] - '0' );
data2 = (data2 << 1) ( out[22] - '0' );
data2 = (data2 << 1) ( out[23] - '0' ); data3 = out[24] - '0';
data3 = (data3 << 1) ( out[25] - '0' );
data3 = (data3 << 1) ( out[26] - '0' );
data3 = (data3 << 1) ( out[27] - '0' );
data3 = (data3 << 1) ( out[28] - '0' );
data3 = (data3 << 1) ( out[29] - '0' );
data3 = (data3 << 1) ( out[30] - '0' );
data3 = (data3 << 1) ( out[31] - '0' ); data4 = out[32] - '0';
data4 = (data4 << 1) ( out[33] - '0' );
data4 = (data4 << 1) ( out[34] - '0' );
data4 = (data4 << 1) ( out[35] - '0' );
data4 = (data4 << 1) ( out[36] - '0' );
data4 = (data4 << 1) ( out[37] - '0' );
data4 = (data4 << 1) ( out[38] - '0' );
data4 = (data4 << 1) ( out[39] - '0' ); data5 = out[40] - '0';
data5 = (data5 << 1) ( out[41] - '0' );
data5 = (data5 << 1) ( out[42] - '0' );
data5 = (data5 << 1) ( out[43] - '0' );
data5 = (data5 << 1) ( out[44] - '0' );
data5 = (data5 << 1) ( out[45] - '0' );
data5 = (data5 << 1) ( out[46] - '0' );
data5 = (data5 << 1) ( out[47] - '0' );
data6 = out[48] - '0';
data6 = (data6 << 1) ( out[49] - '0' );
data6 = (data6 << 1) ( out[50] - '0' );
data6 = (data6 << 1) ( out[51] - '0' );
data6 = (data6 << 1) ( out[52] - '0' );
data6 = (data6 << 1) ( out[53] - '0' );
data6 = (data6 << 1) ( out[54] - '0' );
data6 = (data6 << 1) ( out[55] - '0' ); data7 = out[56] - '0';
data7 = (data7 << 1) ( out[57] - '0' );
data7 = (data7 << 1) ( out[58] - '0' );
data7 = (data7 << 1) ( out[59] - '0' );
data7 = (data7 << 1) ( out[60] - '0' );
data7 = (data7 << 1) ( out[61] - '0' );
data7 = (data7 << 1) ( out[62] - '0' );
data7 = (data7 << 1) ( out[63] - '0' ); in[0] = '0';
in[1] = 'x';
dataTemp = data >> 4;
if(dataTemp <= 9)
{
dataOut = dataTemp '0';
in[2] = dataOut;
}
else
{
dataOut = dataTemp - 10 'A';
in[2] = dataOut;
} dataTemp1 = data & 0x0f;
if(dataTemp1 <= 9)
{
dataOut1 = dataTemp1 '0';
in[3] = dataOut1;
}
else
{
dataOut1 = dataTemp1 -10 'A';
in[3] = dataOut1;
} in[4] = '0';
in[5] = 'x';
dataTemp2 = data1 >> 4;
if(dataTemp2 <= 9)
{
dataOut2 = dataTemp2 '0';
in[6] = dataOut2;
}
else
{
dataOut2 = dataTemp2 - 10 'A';
in[6] = dataOut2;
} dataTemp3 = data1 & 0x0f;
if(dataTemp3 <= 9)
{
dataOut3 = dataTemp3 '0';
in[7] = dataOut3;
}
else
{
dataOut3 = dataTemp3 -10 'A';
in[7] = dataOut3;
} in[8] = '0';
in[9] = 'x';
dataTemp4 = data2 >> 4;
if(dataTemp4 <= 9)
{
dataOut4 = dataTemp4 '0';
in[10] = dataOut4;
}
else
{
dataOut4 = dataTemp4 - 10 'A';
in[10] = dataOut4;
} dataTemp5 = data2 & 0x0f;
if(dataTemp5 <= 9)
{
dataOut5 = dataTemp5 '0';
in[11] = dataOut5;
}
else
{
dataOut5 = dataTemp5 -10 'A';
in[11] = dataOut5;
} in[12] = '0';
in[13] = 'x';
dataTemp6 = data3 >> 4;
if(dataTemp6 <= 9)
{
dataOut6 = dataTemp6 '0';
in[14] = dataOut6;
}
else
{
dataOut6 = dataTemp6 - 10 'A';
in[14] = dataOut6;
} dataTemp7 = data3 & 0x0f;
if(dataTemp7 <= 9)
{
dataOut7 = dataTemp7 '0';
in[15] = dataOut7;
}
else
{
dataOut7 = dataTemp7 -10 'A';
in[15] = dataOut7;
} in[16] = '0';
in[17] = 'x';
dataTemp8 = data4 >> 4;
if(dataTemp8 <= 9)
{
dataOut8 = dataTemp8 '0';
in[18] = dataOut8;
}
else
{
dataOut8 = dataTemp8 - 10 'A';
in[18] = dataOut8;
} dataTemp9 = data4 & 0x0f;
if(dataTemp9 <= 9)
{
dataOut9 = dataTemp9 '0';
in[19] = dataOut9;
}
else
{
dataOut9 = dataTemp9 -10 'A';
in[19] = dataOut9;
} in[20] = '0';
in[21] = 'x';
dataTemp10 = data5 >> 4;
if(dataTemp10 <= 9)
{
dataOut10 = dataTemp10 '0';
in[22] = dataOut10;
}
else
{
dataOut10 = dataTemp10 - 10 'A';
in[22] = dataOut10;
} dataTemp11 = data5 & 0x0f;
if(dataTemp11 <= 9)
{
dataOut11 = dataTemp11 '0';
in[23] = dataOut11;
}
else
{
dataOut11 = dataTemp11 -10 'A';
in[23] = dataOut11;
} in[24] = '0';
in[25] = 'x';
dataTemp12 = data6 >> 4;
if(dataTemp12 <= 9)
{
dataOut12 = dataTemp12 '0';
in[26] = dataOut12;
}
else
{
dataOut12 = dataTemp12 - 10 'A';
in[26] = dataOut12;
} dataTemp13 = data6 & 0x0f;
if(dataTemp13 <= 9)
{
dataOut13 = dataTemp13 '0';
in[27] = dataOut13;
}
else
{
dataOut13 = dataTemp13 -10 'A';
in[27] = dataOut13;
} in[28] = '0';
in[29] = 'x';
dataTemp14 = data7 >> 4;
if(dataTemp14 <= 9)
{
dataOut14 = dataTemp14 '0';
in[30] = dataOut14;
}
else
{
dataOut14 = dataTemp14 - 10 'A';
in[30] = dataOut14;
} dataTemp15 = data7 & 0x0f;
if(dataTemp15 <= 9)
{
dataOut15 = dataTemp15 '0';
in[31] = dataOut15;
}
else
{
dataOut15 = dataTemp15 -10 'A';
in[31] = dataOut15;
} cout << dataOut << dataOut1 << dataOut2 << dataOut3 << endl; fwrite(in, sizeof(char), 32, newCharFile); fclose(charFile);
fclose(newCharFile); return 0;
} 發表人 - scottlcy 於 2003/10/23 01:39:39
|
jcjroc
高階會員
發表:21 回覆:279 積分:115 註冊:2002-09-18
發送簡訊給我
|
我並沒有認真去看你的程式在做啥(看起來好像是從檔案讀取資料然後處裡,而每筆資料的長度32Byte)?而且這個題目看起來像作業
不過我還是提供一些方向給你,
假設你每筆資料長度一樣時,對於此資料可利用struct來定義各欄位
而各欄位用陣列定義其大小,不用每個元素都單獨定義
讀檔時就一次讀一筆資料(struct),一直到file eof為止
還有許多許多的讀檔處理方式,端看你如何運用而已
|
brook
資深會員
發表:57 回覆:323 積分:371 註冊:2002-07-12
發送簡訊給我
|
引言:
我要如何才能使這個while一直讀,每次32個字,然後把全部的值寫回去.
FILE *charFile,*newCharFile;
char in[100];
char input[100]; charFile = fopen("TEST.c", "rb");
if (charFile == NULL) return; //開啟檔案失敗時離開 newCharFile = fopen("REPORT.c", "wb");
if (charFile == NULL)
{
fclose(charFile);
return;
} while(fread(input, 32,1,charFile)==1) //每次讀32bytes,不足32bytes離開
{ // 在這裡放你資料轉換的程式 fwrite(in, 32, 1,newCharFile); //寫32bytes到新檔
} fclose(charFile);
fclose(newCharFile);
|
taishyang
站務副站長
發表:377 回覆:5490 積分:4563 註冊:2002-10-08
發送簡訊給我
|
|