2014-02-08 149 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