원래이 기능은 주 기능에 포함되어있어 매우 복잡한 기능을 제공한다. 프로그램은 탭을 공백 수로 대체합니다. 나는 아직도 내 함수의 인수 목록에 들어가는 것과 argc/argv를 main에서이 함수로 전달하는 방법에 대해 혼란 스럽다. 나는 이것을 올바르게 했는가?필자는 내 기능을 분해하려고 시도했지만 제대로했는지 확신 할 수 없다.
파일의 맨 위에있는 몇 가지 정의 된 변수가 있습니다
#define OUTFILE_NAME "detabbed"
#define TAB_STOP_SIZE 8
#define NUM_ARGS 2
#define FILE_ARG_IDX 1
가 여기 내 두 번째 시도이다는 그것을 :
void open_file(FILE *inf, FILE *outf, char *in[]) /*I feel like the arguments aren't right
{ and this function is just opening
and reading files*/
inf = fopen(in[1], "r");
outf = fopen(OUTFILE_NAME, "w");
if (inf == NULL)
{
perror(in[1]);
exit(1);
}
else if (outf == NULL)
{
perror(OUTFILE_NAME);
exit(1);
}
fclose(inf);
fclose(outf);
}
void detab(FILE *infile, FILE *outfile, char *argument[]) /* Confused about argument list
{ and this function actually
char c; does the detabbing */
int character_count = 0, i, num_spaces;
open_file(infile, outfile, argument); /* I want to call the previous
function but again, confused
while (fscanf(infile, "%c", &c) != EOF) about the argument list */
{
if (c == '\t')
{
num_spaces = TAB_STOP_SIZE - (character_count % TAB_STOP_SIZE);
for (i = 0; i < num_spaces; i++)
{
fprintf(outfile, " ");
}
character_count += num_spaces;
}
else if (c == '\n')
{
fprintf(outfile, "\n");
character_count = 0;
}
else
{
fprintf(outfile, "%c", c);
character_count++;
}
}
}
int main(int argc, char *argv[])
{
if (argc < 1)
{
fprintf(stderr, "usage: prog file\n");
exit(1);
}
else if (argc < NUM_ARGS)
{
fprintf(stderr, "usage: %s file\n", argv[0]);
exit(1);
}
detab(argc, argv); /* I want to pass argc and argv to the detab function, but I'm
having trouble with the argument list */
return 0;
}
나는 간다 무엇인지 파악하고, 도움이 필요 무엇 함수의 인수 목록 내가 혼란스럽게 생각하는 것은 내 인수 유형을 일치 시켜서 한 함수에서 다른 함수로 변수를 전달할 수 있다는 것입니다.
기능의 존재는 나중에 코드 조각을 다시 사용하는 것입니다. 함수가 한 번 호출되는 경우, 나는 이것이 성숙한 최적화라고 생각합니다. –
@Summer_More_More_Tea [번호] (http://blog.regehr.org/archives/942) –
@ H2CO3 감사합니다. –