20131008【C言語】電話帳アプリを作る

お題

電話帳アプリを作る

 

ソース

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

char names[100][40];
char numbers[100][40];

int loc = 0;

void enter(void);
void load(void);
void save(void);
void find(void);

int main(void)
{
 int choice;
 char str[2];
 
 do {
  printf("1. Input name and number.\n");
  printf("2. Search number.\n");
  printf("3. Save phone address to disk file.\n");
  printf("4. Load phone address from disk file.\n");
  printf("5. Exit.\n");
  printf("Enter number for your operation: ");

  gets(str);
  choice = atoi(str);

  switch(choice) {
   case 1:
    enter();
    break;
   case 2:
    find();
    break;
   case 3:
    save();
    break;
   case 4:
    load();
    printf("loc is %d.\n", loc);
  }

 } while(choice != 5);

 exit(0);
}

void save(void)
{
 int i;
 FILE *fp;

 /* Open address.txt for write */
 if *1 == NULL) {
  fprintf(stderr, "Cannot open \"address.txt\" for write.\n"); 
  exit(1);
 }

 /* Write address to address.txt */
 for (i = 0; i <= loc; i++) {
        fprintf(stdout, "%s %s\n", names[i], numbers[i]);
  fprintf(fp, "%s %s\n", names[i], numbers[i]);
 }
}

void find(void)
{
 char name[80];
 int i;
 printf("Please input name you want to find: ");
 gets(name);

 for (i = 0; i <= loc ; i++) {
  if(!strcmp(name, names[i]))
   break;
 }
 printf("%s numbe is %s.\n", name, numbers[i]);
}

void enter(void)
{
 loc++;
 printf("Please input name: ");
 gets(names[loc]);
 printf("Please input number: ");
 gets(numbers[loc]);
}


void load(void)
{
 FILE *fp;
 int i = 0;

 if*2 == NULL) {
  fprintf(stderr, "Cannot open \"address.txt.\".\n");
  exit(1);
 }

 while(!feof(fp)) {
  fscanf(fp, "%s%s", names[i], numbers[i]);
  if (ferror(fp)) {
   fprintf(stderr, "Error while reading \"address.txt\".\n");
   exit(1);
  }
  if(feof(fp))
   break;

  i++;
 }

 loc = i-1;

 /* Close address.txt */
 if (fclose(fp) == EOF) {
  fprintf(stderr, "Error while closing \"address.txt.\"\n");
 }
}

 

実行結果

# cat address.txt
Yamada 111-111-111
Suzuki 222-222-222
Monda 333-333-333
Sugiyama 777-777-777
# ./a.out
1. Input name and number.
2. Search number.
3. Save phone address to disk file.
4. Load phone address from disk file.
5. Exit.
Enter number for your operation: 4
loc is 3.
1. Input name and number.
2. Search number.
3. Save phone address to disk file.
4. Load phone address from disk file.
5. Exit.
Enter number for your operation: 2
Please input name you want to find: Sugiyama
Sugiyama numbe is 777-777-777.
1. Input name and number.
2. Search number.
3. Save phone address to disk file.
4. Load phone address from disk file.
5. Exit.
Enter number for your operation: 1
Please input name: Kaneko
Please input number: 999-999-999
1. Input name and number.
2. Search number.
3. Save phone address to disk file.
4. Load phone address from disk file.
5. Exit.
Enter number for your operation: 3
Yamada 111-111-111
Suzuki 222-222-222
Monda 333-333-333
Sugiyama 777-777-777
Kaneko 999-999-999
1. Input name and number.
2. Search number.
3. Save phone address to disk file.
4. Load phone address from disk file.
5. Exit.
Enter number for your operation: 5
# cat address.txt
Yamada 111-111-111
Suzuki 222-222-222
Monda 333-333-333
Sugiyama 777-777-777
Kaneko 999-999-999

*1:fp = fopen("address.txt", "w"

*2:fp = fopen("address.txt", "r"