구조는 위와같고...
CheckMatching.cpp에서 실행했습니다..
intmain(void)
{
checkMatching("ArrayStack.h");
return0;
}
위와같이 부르는데...
boolcheckMatching(constchar*filename)
{
printf("%s",filename);
FILE *fp =fopen(filename, "r");
if (fp ==NULL)
printf("Error: file not found.\n");
항상 file not found만 뜨네요....checkMatching("./ArrayStack.h")
해도 그럽니다 흐...
******************************************************************************
내용추가
절대 경로로 다 적으니까...아래와같이 나옵니다...허...뭘까요 저런걸 로그로 찍은적이없는데
전체소스입니다
#include"ArrayStack.h"
//주어진 파일을 읽어 괄호 매칭을 검사를 하고 결과를 출력하는 함수
boolcheckMatching(constchar*filename)
{
FILE *fp =fopen(filename, "r");
if (fp ==NULL)
printf("Error: file not found.\n");
int nLine =1; //읽은 라인의 개수
int nChar =0; //읽은 문자 개수
ArrayStack stack; //스택 개체
char ch;
bool quotation=false, bigQuot=false; //작은 따옴표, 큰따옴표
bool note =false; //주석
while ((ch =getc(fp)) != EOF)
{
// if (ch == '\/' && note == false && quotation == false) //작은따옴표에 있는 / 예외
// note = true;
// if (ch == '\'' && quotation == true)
// quotation = false; //처음 작은따옴표를 만나면 작은 따옴표가 시작되었다고 표시하기 위해 true
// else if (ch == '\'')
// quotation = true; //두번째로 만나면 작은 따옴표가 끝났다고 표시하기 위해 false
// if (ch == '\"' && bigQuot == true)
// bigQuot = false; //처음 큰 따옴표를 만나면 큰 따옴표가 시작되었다고 표시하기 위해 true
// else if (ch == '\"')
// bigQuot = true; //두번째로 만나면 큰 따옴표가 끝났다고 표시하기 위해 false
if ((ch =='['|| ch =='('|| ch =='{') && quotation ==false&& bigQuot ==false&& note ==false) //주석이 아니고 작은 따옴표, 큰 따옴표 내에 있는 괄호가 아니라면
stack.push(ch);
if ((ch ==']'|| ch ==')'|| ch =='}') && quotation ==false&& bigQuot ==false&& note ==false) //주석이 아니고 마찬가지로 작은 따옴표, 큰 따옴표 내에 있는 괄호가 아니라면
{
char prev = stack.pop();
if ((prev !='['&& ch ==']') || (prev !='('&& ch ==')') || (prev !='{'&& ch =='}'))
break;
}
if (ch =='\n') //개행되었으므로 주석에서 벗어났다고 간주
{
nLine++;
note =false;
}
nChar++;
}
fclose(fp);
printf("[%s] 파일 검사결과" ,filename);
if (!stack.isEmpty())
printf("[Error 문제 발견 ! 라인수 %d] , 문자수%s" ,nLine,nChar);
else
printf("[ OK: 괄호닫기정상(라인수! 라인수 %d] , 문자수%s" ,nLine,nChar);
return stack.isEmpty();
}
intmain(void)
{
checkMatching("C:\\Users\\user\\Downloads\\hoemwork\\ArrayStack.h");
return0;
}
1. api 혹은 함수에 현재 경로 출력하는 것을 사용하는 방법
2. 임시로 xxxx.ttt 같은 파일을 하나 더미로 쓴 뒤 탐색기로 찾는 방법
둘 중 하나가 있겠네요.