【发布时间】:2025-12-17 14:05:02
【问题描述】:
我正在编写我自己的经典 UNIX 程序“wc”(字数统计)的简化版本。它计算行数、单词数和字符数。所有这些功能都可以正常工作。但是我遇到麻烦的地方是当我试图从 *argv[x] 读取多个文件时。我需要将每个变量都变成一个数组,并通过循环运行整个过程以实现我想要的。
我的程序返回分段错误。在代码中的某个位置没有将某些东西分配到数组中,我似乎无法弄清楚它到底在哪里。
非常感谢任何帮助:)
/*
* [PROGRAM] wc (word count)
* [AUTHOR] Jesper M. Olsen @ jm0.codes
* [DATE] September 9th 2015
* [PURPOSE] Returns number of lines, words, and characters in a file
*
* [DESCRIPTION] This program is meant to be utilized as a handy little browsing tool.
* For instance, while moving through the filesystem of a programming archive,
* just type 'wc <filename>' and you will get number of lines, words and characters returned promptly.
*/
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
if (argc == 1)
return -1;
int numL[argc]; /* initialize array value placeholders */
int numW[argc];
int numC[argc];
int getC[argc];
int getW[argc];
int setNull;
for (setNull = 1; setNull <= argc-1; setNull++) { /* assign ZERO to value placeholders */
numL[setNull] = 0;
numW[setNull] = 0;
numC[setNull] = 0;
getW[setNull] = 0;
}
int x;
FILE *fOp[argc-1];
for (x = 1; x <= argc-1; x++) { /* open file stream for each file */
fOp[x] = fopen(argv[x], "r");
if (fOp[x] == NULL)
return -1;
}
int y;
for (y = 1; (getC[y] = getc(fOp[y])) != EOF; y++) {
if (getC[y] == '\n') numL[y]++;
if (getC[y] == ' ' || getC[y] == '\n' || getC[y] == '\t') getW[y] = 0;
else if (getW[y] == 0) {
getW[y] = 1;
numW[y]++;
} numC[y]++;
}
int z;
for (z = 1; z <= argc-1; z++) { /* close files */
fclose(fOp[z]);
}
int c;
for (c = 1; c <= argc-1; c++) {
printf("[%s] %dL %dW %dC\n", argv[c], numL[c], numW[c], numC[c]);
}
return 0;
}
【问题讨论】:
-
注意 - 您永远不会在代码中使用任何数组的索引
0。 -
请附上调试器回溯输出,以便我们知道段错误在哪一行。
-
FILE *fOp[argc-1];应该是FILE *fOp[argc];我也更喜欢看到< argc而不是<= argc-1 -
所以加载你的调试器并调试程序。
-
代码正在尝试对一个文件执行
wc操作,并且正在尝试对多个文件执行该操作。如果单个文件的wc参数集合在子函数中,则代码会更清晰。从main()中的循环将文件名一次一个地传递给子函数。 一般来说,代码会简单得多,因为数据变量将是单个实例而不是数组。另外,从用户的角度来看,行/字符/单词信息输出到哪里都没有关系,所以在子函数中输出每一行。
标签: c segmentation-fault filestream