c - 我的 C 代码中几乎没有错误吗?

标签 c unix

我的 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 */

最佳答案

这些:

#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[])

关于c - 我的 C 代码中几乎没有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651298/

相关文章:

linux - 如何使 Windows 成为开箱即用的 POSIX 兼容操作系统?

c - 如何在 C 中获取 xterm 的命令行参数?

c - 你将如何有效地实现tail?

无法防止链表打印功能崩溃

linux - Linux 定制版本中的 hp-health-10.50-1826.38.rhel6.x86_64 rpm 软件包出现问题

linux - 验证文件记录 shell 脚本

c - 在 C shell 中实现管道 (Unix)

c - 这是 C 的好替代品吗?

平滑函数的c代码优化

c - 用 C 将用户输入写入文件