コマンドメニュー

コマンド番号を入力することにより、ファイル内容の変更・表示等を行うプログラムです。

コマンドメニュー(list_7.c)
#include <stdio.h>
#include <stdlib.h>

#define F_NAME "ouput.txt"
#define TEMP_F ".tmp"

//-----------------------------------------------------------------
void init_command();
void chop_crlf(char* buff);
int  com_menu();
void del_line();
void add_line();
void disp_line();
int  read_linen();
//-----------------------------------------------------------------

int main()
{
    init_command();
    while(com_menu() != -1);
    return 0;
}
//-----------------------------------------------------------------
// 初期化
//-----------------------------------------------------------------
void init_command()
{
    FILE* fp=fopen(F_NAME,"r");
    if(fp==NULL){
        fp = fopen(F_NAME,"w");
        fclose(fp);
    }
    fclose(fp);
}
//-----------------------------------------------------------------
// 改行コードの除去
//-----------------------------------------------------------------
void chop_crlf(char* buff)
{
    int i,bufleng = strlen(buff);
    for(i=0;i<bufleng;i++){
        if(buff[i] == '\r') buff[i] = 0;
        if(buff[i] == '\n') buff[i] = 0;
    }
}
//-----------------------------------------------------------------
// コマンドメニュー
//-----------------------------------------------------------------
int com_menu()
{
    int i,com; char buf[256];
    printf("\nselect commands:\n");
    printf(" 0: delete line\n");
    printf(" 1: add line\n");
    printf(" 2: disp all line\n");
    printf("-1: end of program..\n");
    printf("**command: ");
    scanf("%s",buf);
    for(i=0;i<strlen(buf);i++)
        if(!isdigit(buf[i]) && buf[i]!='-') return 0;
    sscanf(buf,"%d",&com);
    switch(com){
    case -1: return -1;   break;
    case 0:  del_line();  break;
    case 1:  add_line();  break;
    case 2:  disp_line(); break;
    }
    return 0;
}
//-----------------------------------------------------------------
// 削除
//-----------------------------------------------------------------
void del_line()
{
    FILE *ifp,*ofp;
    int  i,linen,com;
    char buf[256];
    linen = read_linen();
    printf("input line no<0..%d>:",linen-1);
    scanf("%d",&com);
    printf("\n");
    // file
    ifp=fopen(F_NAME,"r");
    ofp=fopen(TEMP_F,"w");
    i = 0;
    // 削除
    while(fgets(buf,256,ifp)!=NULL){
        if(com != i++) fputs(buf,ofp);
    }
    // 後片付け
    fclose(ifp); fclose(ofp);
    unlink(F_NAME);
    rename(TEMP_F,F_NAME);
}
//-----------------------------------------------------------------
// 入力
//-----------------------------------------------------------------
void add_line()
{
    FILE *ifp,*ofp;
    int  i,linen,com;
    char buf[256],instr[256];
    // file
    ifp=fopen(F_NAME,"r");
    ofp=fopen(TEMP_F,"w");
    linen = read_linen();
    if(linen==0){
        printf("add to top of the file.\n");
        com = 0;
    } else {
        printf("input line no<0..%d>:",linen);
        scanf("%d",&com);
    }
    printf("insert string: ");
    scanf("%s",instr);
    // outpu to file
    i = 0;
    if(linen){
        while(fgets(buf,256,ifp)!=NULL){
            if(com==i++) fprintf(ofp,"%s\n",instr);
            chop_crlf(buf);
            fprintf(ofp,"%s\n",buf);
        }
        if(com==linen) fprintf(ofp,"%s\n",instr);
    } else fputs(instr,ofp);
    fclose(ifp); fclose(ofp);
    // 後片付け
    unlink(F_NAME);
    rename(TEMP_F,F_NAME);
}
//-----------------------------------------------------------------
// 表示
//-----------------------------------------------------------------
void disp_line()
{
    int i=0;
    char buf[256];
    FILE *fp=fopen(F_NAME,"r");
    while(fgets(buf,256,fp)!=NULL){
        printf("%d:%s",i++,buf);
    }
    fclose(fp);
}
//-----------------------------------------------------------------
// 読込み
//-----------------------------------------------------------------
int read_linen()
{
    int i=0;
    char buf[256];
    FILE *fp=fopen(F_NAME,"r");
    while(fgets(buf,256,fp)) i++;
    fclose(fp);
    return i;
}

result
Gami[283]% ./list_7.exe

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 2
0:murakami

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 1
input line no<0..1>:0
insert string: akitsugu

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 2
0:akitsugu
1:murakami

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 1
input line no<0..2>:2
insert string: string

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 2
0:akitsugu
1:murakami
2:string

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 0
input line no<0..2>:1


select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: 2
0:akitsugu
1:string

select commands:
 0: delete line
 1: add line
 2: disp all line
-1: end of program..
**command: -1
Gami[284]%
inserted by FC2 system