1 测试功能
??将保存在data.txt中文本数据读取出来,然后均除以0XFE,输出数据为精确到小数点后四位的浮点数,并保存到另一文本文件output.txt中。
输入
?
输出
?
2 测试代码
/*
Func: 将保存在data.txt中文本数据读取出来,
然后均除以0XFE,数据为精确到小数点后四位的浮点数。
,保存到另一文本文件output.txt
Note: 在windows下实现换行需要\r\n,而在UNIX,Linux下只要\n即可;
Author: yicm
Date: 2015-7-18
*/
#include <stdio.h>
#include <string.h>
int Hex_String2_Int(char s[])
{
int i,m,temp=0,n;
m = strlen(s);
for(i=2;i<m;i++)
{
if(s[i]>=‘A‘&&s[i]<=‘F‘)
n=s[i]-‘A‘+10;
else if(s[i]>=‘a‘&&s[i]<=‘f‘)
n=s[i]-‘a‘+10;
else n=s[i]-‘0‘;
temp=temp*16+n;
}
return temp;
}
int main()
{
FILE *fp;
FILE *fp_read;
int temp;
int count;
char dataBuffer[8] = "";
count = 0;
fp = fopen("output.txt","w");
if(NULL == fp){
printf("open file failed!\n");
return -1;
}
fp_read = fopen("data.txt","r");
if(NULL == fp_read){
printf("open file failed!\n");
return -1;
}
int ct = 0;
char buf[5] = "";
float data ;
char ch;
ch = fgetc(fp_read);
while(ch != EOF){
if(ch != ‘,‘){
buf[ct++] = ch;
if(ct == 4){
ct = 0;
data = Hex_String2_Int(buf);
data = data/254;
fprintf(fp,"%.4f\t",data);
printf("%s\t%.4f\t",buf,data);
}
}
else ;
ch = fgetc(fp_read);
if((ch == ‘\r‘) || (ch == ‘\n‘))ch = fgetc(fp_read);
}
fclose(fp);
fclose(fp_read);
return 0;
}
版权声明:本文为博主[原创]文章,未经博主允许可以转载,注明博客出处:[http://blog.csdn.net/FreeApe]
时间: 2024-11-05 01:11:47