# Simple C program to create dictionary for mobile phones.



## sk8harddiefast (Jun 23, 2012)

Sometimes the dictionary we use (for any purpose) is not doing the job. This is a simple dictionary I wrote in C that creates all mobile phone numbers in Greece. In Greece phone numbers are 10 numbers and start from 69. Example: 697 0000 000. You can use it to for you country just changing *for* if you have more or fewer than 10 numbers and *fprintf* if your phones does not start from 69 


```
#include <stdio.h>

main ()
{
int i;
FILE *file;
	file = fopen ("dictnum0", "w");
	for (i=00000000; i<100000000; i++)
	{
		fprintf (file, "69%08d\n", i);
		}
fclose(file);
}
```


----------



## jalla (Jun 23, 2012)

Not to rain on your parade, but there must be a hundred ways to do this on the cmdline already.

One of the lesser known is probably jot(1)()


```
jot 10000000 6900000000
```


----------



## sk8harddiefast (Jun 23, 2012)

Just I made it with C because I like C and I try to learn


----------



## _martin (Jun 23, 2012)

Enjoy learning C 

Note you don't need to use any 0-paddings when initializing i and setting a range. Speaking of range, don't forget that some systems use int sizeof 2 bytes, so way out of your range.

Also some checks on fopen are in order.


----------



## xibo (Jun 25, 2012)

More importantly any function that is not of type void *should* use *return* to leave (i.e. your main that is of type int lacks a return statement), and also symbol type implication has been removed by C99, so you _must_ declare main's return type.


----------



## Zare (Jun 25, 2012)

This is a template that I use for all standalone C programs;


```
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    return EXIT_SUCCESS;
}
```

If you're beginning with C, it's best to type this out of your head, before programming anything that requires main().


----------



## fluca1978 (Jun 25, 2012)

Here is a 5-minutes imrpvoeprovement: the program accepts the prefix from the command line or uses the fixed 69 value. Moreover it builds a format string with the computed padding (not a fixed 8) depending on the length of the prefix. It can be improved much more considering other arguments (e.g., the filename, the number of digits, the number of numbers....). 


```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( int argc, char** argv)
{
  int i;
  char* prefix;
  int prefix_length = 2, max_length = 10;
  int padding = 8;
  int max_numbers = 100000000;
  char* format_string;


  // get the prefix from the command line or from a fixed value
  if( argc > 1 ){
    printf("\nUsing prefix from command line :%s", argv[1] );
    prefix = argv[1];
  }
  else{
    prefix = "69";
    printf("\nUsing builtin prefix %s", prefix);
    
  }

  // get the prefix length
  prefix_length = strlen( prefix );
  // compute the padding
  padding = max_length - prefix_length;
  printf("\nPrefix is %d digits, max length is %d and padding is %d digits\n", prefix_length, max_length, padding);

  // the format string will be <prefix>%0<padding>d
  format_string = malloc( sizeof(char) * (prefix_length + 2 + 1 + 1 + 1 + 1 ) );
  sprintf( format_string, "%s%%0%dd\n", prefix, padding );
  printf("\nFormat string is [ %s ]", format_string );
  


  FILE *file;
  file = fopen ("dictnum0", "w");
  for (i=0; i < max_numbers; i++)
    {
      fprintf ( file, format_string , i);
    }
  fclose(file);

  return 0;
}
```


----------



## expl (Jun 25, 2012)

If you are just dumping data to a file in a sequence it*'*s faster to use direct I/O instead of buffered one to avoid data duplication in your memory since all modern filesystems will cache the writes to memory anyways.


----------

