求助,怎么把数字以数值形式写入文件,而不是以文本形式写入文件
如上图的文本文件, 我想把""的字符串部分以文本形式写入文件,后面的数字以数值形式写入文件。但是程序运行完后发现数字部分也是文本的形式了,求助该怎么把数字以数值的形式写入文件
下面是源代码
程序代码:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char inputname[256], *outputname, tempstring[256];
char number[11] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
int j = 0;
int m = 0;
int flag = 0;
int n;
char cachestring[256][256];
char line[256];
int strcount = 0;
FILE *fpinput, *fpoutput;
puts("请输入要转换的json文件");
gets(inputname);
if((fpinput = fopen(inputname, "r+")) == NULL)
{
printf("无法打开%s,请检查文件名是否正确,以及文件是否在目录", inputname);
exit(EXIT_FAILURE);
}
outputname = strcat(inputname, ".rton");
if((fpoutput = fopen(outputname, "w+")) == NULL)
{
printf("无法创建%s", outputname);
exit(EXIT_FAILURE);
}
fputs("RTON", fpoutput);
putc(0x01, fpoutput);
putc(0x00, fpoutput);
putc(0x00, fpoutput);
putc(0x00, fpoutput);
while(fgets(line, 256, fpinput) != NULL)
{
for(i = 0; i < strlen(line); i++)
{
if(line[i] == '\"')
{
i++;
for(j = 0, strcount = 0;line[i] != '\"'; j++, i++, strcount++)
{
cachestring[m][j] = line[i];
}
putc(0x90, fpoutput);
putc(strcount, fpoutput);
fputs(cachestring[m], fpoutput);
m++;
}
for(n = 0; n < 11; n++)
{
if(line[i] == number[n])
{
for(j = 0, strcount = 0; line[i] != ','; j++, i++, strcount++)
{
tempstring[j] = line[i];
}
flag = atoi(tempstring);
fprintf(fpoutput, "%x", flag);
break;
}
}
}
}
fclose(fpinput);
fclose(fpoutput);
return 0;
}
[此贴子已经被作者于2018-9-22 17:57编辑过]







