数据存储格式转换
功能:
将一个文件内容为"aa bb cc dd ee ff"等大量数据的文件转换为
aa
bb
cc
dd
ee
ff
的文件中。
测试源文件为all_send.txt.
C语言源代码如下:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i = 0, count = 0;
char buffer[1024] = {0};
char buff[64] = {0};
// 读取文件
FILE *fp = NULL;
FILE *fpw = NULL;
fp = fopen("all_send.txt", "r+");
if(!fp)
{
printf("origin file open error !\n");
return -1;
}
fpw = fopen("all_send_after.txt", "w+");
if(!fpw)
{
printf("write file can't open!\n");
return -1;
}
// 读取文件中的每一行进行转换,然后写入到写入文件中
while(!feof(fp))
{
fgets(buffer, 1024, fp);
for(i=0; i<strlen(buffer);)
{
//printf("%c\n", buffer[i]);
if(buffer[i] != ' ')
{
count++;
sprintf(buff, "%c%c\n", buffer[i], buffer[i+1]);
fputs(buff, fpw);
i += 2;
}
else
{
//sprintf(buff,"\n");
//fputs(buff, fpw);
// do nothing to the write file
i += 1;
}
}
//msleep(100);
}
printf("count : %d\n", count);
fclose(fp);
fclose(fpw);
return 0;
}