我坚持使用我的C程序,但我遇到了一些错误。我对C的经验很少,而且我正在编写一个程序来模拟Linux / Unix中的wc命令。我尝试过很多东西试图让我的代码工作,但到目前为止没有任何帮助我。这是我的代码,其中包含错误:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
/* Enumerators */
enum { FALSE, TRUE };
enum { STDIN, STDOUT, STDERR };
#define BUFFER_SIZE 4096];
#define NAME_SIZE 12];
#define MAX_LINES 100000];
/* Globals */
char *fileName = NULL;
char tmpName [NAME_SIZE];
int option = FALSE;
int charOption = FALSE; // There is not a character option
int wordOption = FALSE; // There is not a word option
int lineOption = FALSE; // There is not a line option
int standardInput = FALSE;
int c = 0; // Character to be scanned
int nl = 0; // Number of lines
int nw = 0; // Number of words
int nc = 0; // Number of characters
int fileOffset = 0;
int fd;
parseCommandLine(int argc, char* argv[]) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-')
processOptions(argv[i]);
else if (fileName == NULL)
fileName = argv[i];
else
usageError();
}
standardInput = (fileName == NULL);
}
processOptions(char* str) {
int j;
for (j = 1; str[j] != NULL; j++) {
switch (str[j]) {
case 'c': /* Count number of characters in a file */
charOption = TRUE; /* Option has been supplied in command line */
while (getchar() != EOF) /* Scan characters until end of file */
nc++; /* Increment number of characters */
break;
case 'l': /* Count number of lines in a file */
charOption = TRUE; /* Option has been supplied in command line */
while ((c == getchar()) != 'Z') /* Scan chars until end of file */
if (c == '\n')
nl++;
break;
case 'w': /* Count number of words in a file */
charOption = TRUE; /* Option has been supplied in command line */
int state = 0;
while ((c == getchar() != 'Z')) {
++nc;
if (c == '\n') /* New line */
nl++; /* Increment number of lines */
if (c == ' ' || c == '\n' || c == '\t') /* Word separators */
state = 0;
else if (state == 0) {
state = 1;
++nw; /* Increment number of words */
}
} /* End while */
break;
default:
usageError(); /* An error occurred */
break;
}
}
}
/* Too many arguments supplied */
usageError() {
fprintf(stderr, "Usage: mywc -lwc [fileName]"\)n"");
exit(1);
;}
/* Main program */
main(argc, argv[])
char* argv[];
({
int i;
processOptions(str); /* Read options from command line */
int flag[3] = {lineOption,wordOption,charOption}; /* Count options */
int stats[3] = {nl,nw,nc}; /* Number of lines, words, and chars in file */
if (!option) /* No options have been supplied; print number of lines, words, and characters in a file */
printf("%d %d %d %s\n",nl,nw,nc,fileName);
else { /* Options have been supplied, read them */
for (i = 0; i < 3; i++) {
if (flag[i]) /* If a specific argument is supplied */
printf("%d ",stats[i]); /* Print number of lines, words, and/or chars, depending on the argument(s) supplied */
}
printf("%s\n",fileName); /* Print file name */
}
return 0; /* Exit program */
}
然后,当我去编译我的程序时,我收到这些错误/警告:
mywc.c:19:24: error: expected identifier or ‘(’ before ‘]’ token
char tmpName [NAME_SIZE]
^
mywc.c:29:24: error: expected identifier or ‘(’ before ‘]’ token
int lineStart[MAX_LINES]
^
mywc.c: In function ‘processOptions’:
mywc.c:50:24: warning: comparison between pointer and integer
for (j = 1; str[j] != NULL; j++) {
^
mywc.c: In function ‘usageError’:
mywc.c:86:5: error: stray ‘\’ in program
fprintf(stderr, "Usage: mywc -lwc [fileName]"\)n"");
^
mywc.c:86:52: error: expected ‘;’ before ‘n’
fprintf(stderr, "Usage: mywc -lwc [fileName]"\)n"");
^
mywc.c:86:55: error: expected statement before ‘)’ token
fprintf(stderr, "Usage: mywc -lwc [fileName]"\)n"");
^
mywc.c: At top level:
mywc.c:90:16: error: expected ‘)’ before ‘[’ token
main(argc, argv[])
^
mywc.c:92:2: error: expected identifier or ‘(’ before ‘{’ token
({
^
正如我上面所说,我尝试了很多看似正确的事情,但这些也给了我错误。我将非常感谢能够解决这些错误的任何帮助,以及一些帮助我使程序正常运行的提示。
PS假设我有一个看起来像这样的test.txt文件:
Hello World!
这是我的程序应该是什么样的输出示例:
./mywc -c test.txt
12 /* Assuming that test.txt contains 8 characters */
./mywc -l test.txt
1
./mywc test.txt
1 2 12 test.txt /* 1 is number of lines, 2 is number of words, 12 is number of chars */
答案 0 :(得分:1)
这些:
#define BUFFER_SIZE 4096];
#define NAME_SIZE 12];
#define MAX_LINES 100000];
当您使用符号时,将提供解析错误,就像它们是普通整数一样。从每个方格中删除尾随方括号和分号。
此:
fprintf(stderr, "Usage: mywc -lwc [fileName]"\)n"");
应该是
fprintf(stderr, "Usage: mywc -lwc [fileName]\n");
此:
main(argc, argv[])
应该是:
main(int argc, char *argv[])