코딩 과제 중 질문입니다

익명 사용자의 이미지

알고리즘 코딩 과제를 진행중입니다.

Visual C++ 컴파일 시 바로 실행할 수 있게 하라는데 바로 실행할 시 input할 텍스트파일을 읽어와야 합니다.

파일을 어느 폴더에서 읽어오는 지를 몰라서 이 환경에서 테스트를 못해보고 있습니다.

해당 프로젝트 폴더의 .exe가 들어있는 debug 폴더에 텍스트 파일을 넣어봐도 읽지 못하고,

심지어 debug폴더 안의 .exe파일을 실행하면 읽고 수행은 되지만 output.txt가 빈 파일로 출력됩니다.

C:\test 를 임시로 만들고 거기다 .exe를 넣고 돌리니까 정상 출력이 됩니다.

Q 1. VB 컴파일 수행 시 어느 폴더에서 파일을 읽어올 수 있는지 궁금합니다.
실행자가 input 파일 이름을 직접 입력한다는 조건도 붙어있어서 코드는 이렇습니다.

 
printf("input file name? ");
scanf("%s", input_str);
 
input_file = fopen(input_str, "r");
if (input_file == NULL)
{
	fprintf(stderr, "File %s not found\n", input_str);
	return 0;
}

Q 2. debug 폴더에서는 왜 수행해도 output.txt 가 공백파일인지 모르겠습니다.
코드 원문입니다.

#include <stdio.h>
#include <malloc.h>
#define INFY 987654321;
 
FILE *input_file, *output_file;
int **M, **P;
 
int** array_malloc(int n)
{
	int** array;
	int i, j;
 
	/* array allocate */
	array = (int**) malloc(0);
	for (i = 0; i <= n; i++)
		array[i] = (int*) malloc(sizeof(int) * n);
 
	/* init array */
	for (i = 1; i <= n; i++)
		for (j = 1; j <= n; j++)
			array[i][j] = 0;
 
	return array;
}
 
void print_order(int i, int j)
{
	int n;
 
	if (i == j)
		fprintf(output_file, "A%d ", i);
	else
	{
		n = P[i][j];
		fprintf(output_file, "( ");
		print_order(i, n);
		print_order(n + 1, j);
		fprintf(output_file, " )");
	}
}
 
void minmult(int n, int* d)
{
	int i, j, k, q;
 
	M = array_malloc(n);
	for (i = 1; i <= n; i++)
	{
		for (j = i + 1; j <= n; j++)
		{
			M[i][i] = 0;
			M[i][j] = INFY;
		}
	}
 
	for (i = n; i > 0; i--)
	{
		for (j = i; j <= n; j++)
		{
			if (i == j)
				M[i][j] = 0;
			else
			{
				for (k = i; k < j; k++)
				{
					q = M[i][k] + M[k+1][j] + d[i-1] * d[k] * d[j];
					if (q < M[i][j])
					{
						M[i][j] = q;
						P[i][j] = k;
					}
				}
			}
		}
	}
}
 
int main()
{
	char input_str[100];
	int *d;
	int input, n, k;
 
	printf("input file name? ");
	scanf("%s", input_str);
 
	input_file = fopen(input_str, "r");
	if (input_file == NULL)
	{
		fprintf(stderr, "File %s not found\n", input_str);
		return 0;
	}
	output_file = fopen("output.txt", "w");
 
	fscanf(input_file, "%d", &input);
	n = input - 1;
	d = (int*) malloc(sizeof(int) * n);
	for (k = 0; k < n; k++)
	{
		if (fscanf(input_file, "%d", &d[k]) == -1)
		{
			d[k] = 0;
			n = k - 1;
			break;
		}
	}
 
	P = array_malloc(n);
 
	minmult(n, d);
 
	print_order(1, n);
 
	fclose(input_file);
	fclose(output_file);
 
	return 0;
}

chaoty의 이미지

프로그램을 exe 파일을 실행시키면 exe파일이 있는 경로가 기본 경로가 됩니다. 따라서 입출력 할 파일명을 입력하면 exe 파일이 있는 곳으로 입출력을 수행합니다.
만약 visual studio로 실행했을 경우 vcxproj 파일이 있는 곳이 기본 경로가 됩니다. 따라서 입출력할 파일명을 입력하면 vcxproj 파일이 있는 곳으로 입출력을 수행합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.