1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* reverse: reverse string s in place */
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* itoa: convert n to characters in s */
// change from integer to a character
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
// returns the number of digits in the number
int num_of_digits(int x)
{
int count = 0;
while (x>0)
{
x = x/10;
count++;
}
return count;
}
// creates the names for the files
void createNames(char name[][12], int size)
{
char n[4];
int digit = 0;
for (int i=0; i<(size/2)+1; i++)
{
// inserting "file"
name[i][0] = 'f';
name[i][1] = 'i';
name[i][2] = 'l';
name[i][3] = 'e';
itoa(i, n);
digit = num_of_digits(i);
// inserting a "number"
if (digit <= 1)
{
name[i][4] = '0';
name[i][5] = '0';
name[i][6] = '0';
name[i][7] = n[0];
}
else if (digit == 2)
{
name[i][4] = '0';
name[i][5] = '0';
name[i][6] = n[1];
name[i][7] = n[0];
}
else if (digit == 3)
{
name[i][4] = '0';
name[i][5] = n[2];
name[i][6] = n[1];
name[i][7] = n[0];
}
else if (digit == 4)
{
name[i][4] = n[3];
name[i][5] = n[2];
name[i][6] = n[1];
name[i][7] = n[0];
}
// inserting the ".txt"
name[i][8] = '.';
name[i][9] = 't';
name[i][10] = 'x';
name[i][11] = 't';
}
}
// creates new files
void createFiles(int size)
{
FILE * pFile[6];
char names[6][12];
createNames(names, size);
for (int i=0; i<6; i++)
{
pFile[i] = fopen(names[i], "w");
}
}
int main ()
{
int size = 10;
char name[6][12]; // an array of file names
createNames(name, size);
createFiles(size);
// printing the files names
for (int i=0; i<6; i++)
{
for (int j=0; j<12; j++)
printf("%c",name[i][j]);
printf("\n");
}
return 0;
}
|