You posted this three times in two minutes. Before assuming that a posting
failed and retrying to post the same article you should allow some time
for the post to arrive.
LilacSkin said:
The appropriate format is :
(1st signed long long) (tab) (2nd signed long long) (tab) ... (16th
signed long long) (\n)
(17th signed long long) .....
what I've done :
#include <stdio.h>
#include <time.h>
void convert(long long x,char *s,int sz)
{ char *p = s + sz;
{ char *p = s + sz;
int sign = x < 0;
if (sign) x = -x;
*p-- = '\0';
do
{ *p-- = '0' + (x % 10);
} while ((p >= s) && (x /= 10));
while (p >= s) *p-- = ' ';
if (sign) *s = '-';
}
int digits(long long x)
{ int n = 1;
do
{ n++;
} while (x /= 10);
return n;
} See comments at the bottom.
int main(void)
{
time_t start_time;
time_t end_time;
double diff_time = 0;
int i;
int num_digit;
char buffer[64];
long long test = -1844674407370955161;
FILE * pFile = NULL;
pFile = fopen( "data_test_write.txt", "w" );
How do you know that it didn't fail?
time ( &start_time );
for (i=0;i<16000000;i++)
That isn't required to fit in an int. If you mind portability you should
declare i as long if you know it's going to hold numbers > 32767.
{
num_digit=digits(test);
convert(test,buffer,num_digit);
//printf("'%s'\n",buffer);
fwrite (buffer , 1 , num_digit, pFile );
You can use fprintf with "lld":
fprintf(pFile, "%lld", test);
Its implementer probably has used the fastest possible routine to convert
a number to a decimal representation on your system.
if (i % 16 == 15) fputc ((int) '\n' , pFile );
That cast is useless. '\n' already has type int, and if it hadn't it would
be converted to one.
else fputc ((int) '\t' , pFile );
Look up the ?: operator. While abusing it can make code unreadable,
moderately using it can be handy when otherwise you'd have almost
identical if and else branches.
fputc( (i % 16 == 15) ? '\n' : '\t', pFile);
}
time ( &end_time );
diff_time = difftime ( end_time, start_time );
printf("Duration : %lf sec\n", diff_time);
printf("Num of samples (16*64bit + /t and /n) : %i\n", i/16);
return 0;
}
The problem is, there is a space character before a positive number.
Your digits() function return a number too large by one. For negative
numbers, the extra character is used by the minus sign.