該代碼讀取一個帶十六進制格式數字的csv文件,將它們轉換爲十進制格式並將十進制數寫入另一個文件。將十六進制數轉換爲十進制格式
/* Author: Madalina Erascu*/
/* Input: csv file with numbers in hexa fomat
Output: csv file with numbers in decimal fomat with 80 decimals
Usage: main list_input_files list_output_files
Attention: function hex2double - the specifier llx might not give the same output on computers with other OS than Windows, MinGW compiler, etc.
*/
#include <stdio.h>
#include <stdlib.h>
long double hex2double(const char *);
int main(int argc, char *argv[])
{
char buffer[1024] ;
char *record,*line;
long double x;
int i,j;
printf("argc = %d\n", argc);
for(i = 1; i <= argc/2; ++i) {
printf("argv[ %d ] = %s\n", i, argv[ i ]);
printf("argv[ %d ] = %s\n", (argc/2) + i, argv[ (argc/2) + i ]);
FILE *fstream_in = fopen(argv[ i ],"r");
FILE *fstream_out = fopen(argv[ (argc/2) + i ],"w");
if(fstream_in == NULL) {
printf("\nFile opening failed:", fstream_in);
return -1 ;
}
int idx = 0;
while((line=fgets(buffer,sizeof(buffer),fstream_in))!=NULL) {
record = strtok(line,",");
while(record != NULL) {
idx++;
printf("String : %d|%s|\n",idx,record) ;
x = hex2double(record) ;
fprintf(fstream_out,"%.80e\n", x);
record = strtok(NULL,",");
}
}
}
return 0 ;
}
long double hex2double(const char *s)
{
long double d = 0.0;
sscanf(s, "%llx", &d);
return d;
}
當我使用該文件在此鏈接輸入:
https://dl.dropboxusercontent.com/u/64621064/trigon_batch2.csv。
文件中的第61個數字被分成2個數字。 hexa格式的數字實際上是浮點數,不是整數...
任何解釋?
'unsigned long long'sscanf的'llx'。 – BLUEPIXY
這個'printf(「\ nFile打開失敗:」,fstream_in);'是未定義的行爲!爲什麼你將流作爲參數傳遞? –