電話帳------コマンドと入力パラメータを分離

2352 ワード

// 2.16.cpp :              。
//

#include "stdafx.h"
#include <windows.h>
#include <string.h>

const unsigned int MAX_LENGTH_OF_CMD = 300;
const char* DEFAULT_FILE_NAME = "temp.txt";

/*
strDefaultFileFullPath          ,   MAX_PATH
*/
bool GetDefaultFileFullPath(char *strDefaultFileFullPath, const char *strDefaultFileName, const char *strArgv0)
{
	int i = strlen(strArgv0) - 1;
	while(i >= 0)
	{
		if(strArgv0[i] == '\\')
			break;

		i--;
	}

	if(i < 0)
	{
		strcpy(strDefaultFileFullPath, strDefaultFileName);
	}
	else
	{
		strcpy(strDefaultFileFullPath, strArgv0);
		strcpy(strDefaultFileFullPath + i + 1, strDefaultFileName);
	}

	return true;
}

void DisplayDefaultFile(const char *strDefaultFileFullPath)
{
	FILE *fp = fopen(strDefaultFileFullPath, "r+");
	if(fp != NULL)
	{
		while(1)
		{
			char buf[MAX_PATH];
			if(fgets(buf, MAX_PATH, fp) == NULL)
				break;

			printf("%s", buf);
		}

		fclose(fp);
	}
	else
	{
		printf("Default file does not exist.
"); } } void EnterCommandLoop() { char cmd[MAX_LENGTH_OF_CMD]; while(1) { printf("Please input your command : "); if(scanf("%[^
]", cmd) == 0) { fflush(stdin); continue; } fflush(stdin); // 3 , ‘\0’ cmd[3] = 0; printf("Your command is : %s
", cmd); if(strcmp(cmd, "-ld") == 0) { printf("will import phone book
"); char file[MAX_PATH]; strcpy(file, cmd + 4); printf("Load file needed is: %s
", file); continue; } if(strcmp(cmd, "-sa") == 0) { printf("will export phone book
"); continue; } if(strcmp(cmd, "-se") == 0) { printf("will save phone book
"); continue; } if(strcmp(cmd, "-ad") == 0) { printf("will add item(name phone)
"); continue; } if(strcmp(cmd, "-qt") == 0) { printf("quit now ....
"); break; } } } int _tmain(int argc, _TCHAR* argv[]) { char DefaultFileFullPath[MAX_PATH]; GetDefaultFileFullPath(DefaultFileFullPath, DEFAULT_FILE_NAME, argv[0]); printf("Default file path: %s
", DefaultFileFullPath); DisplayDefaultFile(DefaultFileFullPath); EnterCommandLoop(); return 0; }