C implementation creates multiple txt file and named to the natural numbers, then the resulting decimal data written txt file

First of all: a natural number as a txt named.

It's easy to implement in C ++, between eh to_string (int num) + ". Txt" can be, but there is no string in C, only char, how to do that?

It should be a natural number converted into char, char array to be exact. Code:

char * Int2String ( int num, char * STR) // 10 decimal 
{
     int I = 0 ; // indicates filling STR 
    IF (num < 0 ) // If num is negative, the num becomes positive 
    { 
        num = - num; 
        STR [I ++] = ' - ' ; 
    } 
    // convert 
    do 
    { 
        STR [I ++] = num% 10 + 48 ; // get the lowest num characters 0 to 9 ASCII code is 48 to 57; easy to said digital 0 + 48 = 48, ASCII character code corresponding to '0' 
        NUM / =10 ; // remove the least significant bits     
    } the while (NUM); // NUM is not 0 continues to loop     
    STR [I] = ' \ 0 ' ;    
     // determine the start position of adjustment 
    int J = 0 ;
     IF (STR [ 0 ] = = ' - ' ) // if there is a negative number, the negative number without adjustment 
    { 
        J = 1 ; // second adjustment starts from 
        ++ I; // Since the negative sign, so that the axis of symmetry of the exchanger 1 also shifted bit 
    }
     // symmetric swapping 
    for (; J <I / 2 ; J ++ )
    { 
        // symmetric swapping value actually saved across the intermediate variable exchange a + b values: A = a + b; b = ab &; A = ab &; 
        STR [J] = STR [J] + STR [I- . 1 - J]; 
        STR [I - . 1 -j] = STR [J] - STR [I- . 1 - J]; 
        STR [J] = STR [J] - STR [I- . 1 - J]; 
    }      
    return STR; // value of the conversion returned 
}

Well, this step is done, the file name can be resolved.

The following is a decimal number written txt, here to distinguish between two functions:

fprintf (FP, " % D " , Buffer); formatted data is written to the file 
fprintf (file pointer, format string, output table column); 

fwrite ( & Buffer, the sizeof ( int ), . 1 , FP); is writing files in binary mode 
fwrite (data, the data size of the maximum number (number of bytes), the write data, the file pointer);

fprintf band data is written to the file format, the same effect is the decimal metric, printing and writing files.

fwrite is a natural number is written to the file in binary mode, char unchanged.

The desire to help you doubt!

Guess you like

Origin www.cnblogs.com/zhibei/p/12061215.html