#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ENDS_WITH(ch0, ch1) ((unsigned char)line[len - 3] == (ch1) && (unsigned char)line[len - 4] == (ch0))
/*
* for gbk only.
*/
int main()
{
FILE* fp = fopen("/mnt/hgfs/share/aaa1.txt", "r");
FILE* fpOut = fopen("/mnt/hgfs/share/aaa2.txt", "w+");
if (NULL == fp)
return -1;
char line[1024 * 10] = {0};
while (NULL != fgets(line, sizeof(line) - 1, fp))
{
int len = strlen(line);
if (0) // (len > 10)
{
printf("[%s]\n-1:%x\n-2:%x\n-3:%x\n-4:%x\n-5:%x\n-6:%x\n",
line,
(unsigned int)(unsigned char)line[len - 1],
(unsigned int)(unsigned char)line[len - 2],
(unsigned int)(unsigned char)line[len - 3],
(unsigned int)(unsigned char)line[len - 4],
(unsigned int)(unsigned char)line[len - 5],
(unsigned int)(unsigned char)line[len - 6]);
}
if (len >= 4)
{
do {
if ((unsigned char)line[len - 1] != 0xa
|| (unsigned char)line[len - 2] != 0xd)
break;
if (ENDS_WITH(0x80, 0x82))
{
// period
break;
}
if (ENDS_WITH(0x80, 0x9d))
{
// double quotation mark
break;
}
line[len - 2] = 0;
} while (0);
}
fputs(line, fpOut);
}
fclose(fpOut);
fclose(fp);
return 1;
} |
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ENDS_WITH(ch0, ch1) ((unsigned char)line[len - 3] == (ch1) && (unsigned char)line[len - 4] == (ch0))
/*
* for gbk only.
*/
int main()
{
FILE* fp = fopen("/mnt/hgfs/share/aaa1.txt", "r");
FILE* fpOut = fopen("/mnt/hgfs/share/aaa2.txt", "w+");
if (NULL == fp)
return -1;
char line[1024 * 10] = {0};
while (NULL != fgets(line, sizeof(line) - 1, fp))
{
int len = strlen(line);
if (0) // (len > 10)
{
printf("[%s]\n-1:%x\n-2:%x\n-3:%x\n-4:%x\n-5:%x\n-6:%x\n",
line,
(unsigned int)(unsigned char)line[len - 1],
(unsigned int)(unsigned char)line[len - 2],
(unsigned int)(unsigned char)line[len - 3],
(unsigned int)(unsigned char)line[len - 4],
(unsigned int)(unsigned char)line[len - 5],
(unsigned int)(unsigned char)line[len - 6]);
}
if (len >= 4)
{
do {
if ((unsigned char)line[len - 1] != 0xa
|| (unsigned char)line[len - 2] != 0xd)
break;
if (ENDS_WITH(0x80, 0x82))
{
// period
break;
}
if (ENDS_WITH(0x80, 0x9d))
{
// double quotation mark
break;
}
line[len - 2] = 0;
} while (0);
}
fputs(line, fpOut);
}
fclose(fpOut);
fclose(fp);
return 1;
}