2014-02-08 127 views
0

我困在这个c程序的前半部分,我正在用vim写cygwin,我需要将文件内容打印到标准输出。我只是想确保我的getNextWord函数正常工作,但是当我编译时,我得到的代码后会显示错误。这是迄今为止我所拥有的。如何打印c文件的内容?

#include <stdio.h> 
#include <ctype.h> 
#include <stdlib.h> 
#include <string.h> 

int MAX_WORD_LEN = 256; 

char* getNextWord(FILE* fd) { 
    char c; 
    char wordBuffer[MAX_WORD_LEN]; 
    int putChar = 0; 

    while((c = fgetc(fd)) != EOF) { 
     if(isalum(c)) break; 
    } 
    if (c == EOF) return NULL; 

    wordBuffer[putChar++] = tolower(c); 

    while((c = fgetc(fd)) != EOF) { 
     if(isspace(c) || putChar >= MAX_WORD_LEN -1) break; 

     if(isalum(c)) { 
      wordBuffer[putChar++] = tolower(c); 
     } 
    } 
    wordBuffer[putChar] = '\0'; 
    return strdup(wordBuffer); 
} 

int main() { 
    char filename[50]; 
    printf("Enter the file name: \n"); 
    scanf("%s", filename); 
    FILE *file = fopen(filename, "r"); 
    getNextWord(file); 
    fclose(file); 

    return 0; 
} 

所以这里就是我在shell看到,下一行显示的命令行:

$gcc words.c -o words 

/tmp/ccku5u4f.o:words.c:(.text+0x70): undefined reference to 'isalum' 

/tmp/ccku5u4f.o:words.c:(.text+0x70): relocation truncated to fit: R_X86_64_PC32 against 
undefined symbol 'isalum' 

/tmp/ccku5u4f.o:words.c:(.text+0xfd): undefined reference to 'isalum' 

/tmp/ccku5u4f.o:words.c:(.text+0xfd): relocation truncated to fit: R_X86_64_PC32 against 
undefined symbol 'isalum' 

/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: /tmp/cc/ko5u4f.o: 
bad reloc address 0x0 in section '.pdata' 

/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: final l ink failed: 
Invalid operation 

collect2: error: ld return 1 exit status 

我为如何处理这些错误完全一无所知。任何帮助将不胜感激。

+0

哪里是isalum –

+0

编译警告的定义,例如'gcc -Wall -Wextra -Werror -o foo foo.c' – Brandin

回答

3

我想你的意思isalnum代替isalum ...

+0

哇...非常感谢你。现在它编译好了,但运行后我没有在控制台上得到任何东西。如何使用我的getNextWord函数实际显示输出? – Josh