最初,这个函数被嵌入到主函数中,创建一个非常混乱的主函数。该程序将使用空格数替换制表符。我仍然对我的函数的参数列表内部以及如何将main中的argc/argv传递给这些函数感到困惑。我做对了吗?我试图分解我的功能,但我不知道如果我做对了
有在文件的顶部部分定义的变量:
#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) – 2013-06-01 07:23:09
@ H2CO3谢谢,请看看。 –