当前位置: 代码迷 >> C语言 >> 请教C语言转.DBF为.txt问题
  详细解决方案

请教C语言转.DBF为.txt问题

热度:185   发布时间:2007-09-20 09:41:53.0
请教C语言转.DBF为.txt问题
我用C语言in = fopen(infilename, "rb"); out = fopen(outfilename, "wb");分别打开要转换的数据库(dbf)与要生成的文本文件(txt). 用 fputc(fgetc(in), out);输出到文件本文件.
可是输出时会将空格变成乱码.哪位高手过来指点下,感激不尽!
搜索更多相关的解决方案: DBF  C语言  txt  数据库  fgetc  

----------------解决方案--------------------------------------------------------

有没人帮下忙啊


----------------解决方案--------------------------------------------------------
我也想知道!
个人猜测是:因为dbf是数据库文件txt是文本文件,数据库文件要专业的软件才可打开把!
只是单纯的变为txt就跟有记事本打开dbf文件一样:乱码!

纯属个人猜测!!!
----------------解决方案--------------------------------------------------------
DBF显然不会是文本文档。就像word的.doc是二进制文件一样。
----------------解决方案--------------------------------------------------------

经过努力终于完成了.DBF转换成.xls和.txt,谢谢各位.代码如下:各位高手有什么好建议请提下!!!
#define M 100
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

void main()
{
FILE *fp, *out1, *out2;
unsigned int recno, headlength, reclength;
unsigned long recnumber;
unsigned char t1, t2, t3, t4;
char fname[13], str[M], oname[13], outname[13];
int i;
printf("请输入要转换的\n");
printf("文件名(.dbf):\n");
gets(fname);
printf("输出文件(.txt):\n");
gets(oname);
printf("输出文件(.xls):\n");
gets(outname);

if(strchr(fname, 0x2e) == NULL)
{
strcat(fname, ".dbf");
}
if(strchr(oname, 0x2e) == NULL)
{
strcat(oname, ".txt");
}
if(strchr(outname, 0x2e) == NULL)
{
strcat(outname, ".xls");
}

if((fp = fopen(fname, "rb+")) == NULL)
{
printf("文件不存在%s\n", fname);
getch();
exit(0);
}

if((out1 = fopen(outname, "wb+")) == NULL)
{
printf("不能打开文件%s\n", outname);
getch();
exit(0);
}

if((out2 = fopen(oname, "wb+")) ==NULL)
{
printf("不能打开文件%s\n", oname);
getch();
exit(0);
}

while(! feof(fp))
{
fputc(fgetc(fp), out1);
}
if (fputc(fgetc(fp), out1) != EOF)
{
printf("转换xls成功\n");
}

fseek(fp, 4L, 0);
fscanf(fp, "%c%c%c%c", &t1, &t2, &t3, &t4);
recnumber = t1 + t2*0x100 + t3*0x10000 + t4*0x1000000;
fseek(fp, 8L, 0);
fscanf(fp, "%c%c", &t1, &t2);
headlength = t1 + t2*0x100;
fseek(fp, 10L, 0);
fscanf(fp, "%c%c", &t1, &t2);
reclength = t1 + t2*0x100;
fseek(fp, headlength, 0);

for(recno = 1; recno <= recnumber; recno++)
{
fgets(str, reclength+1, fp);
fputs(str, out2);
}

if (fputs(str, out2) != EOF)
{
printf("转换txt成功\n");
}
fclose(fp);
fclose(out1);
fclose(out2);
getch();
}


----------------解决方案--------------------------------------------------------
  相关解决方案