繁体   English   中英

如何在c中打印文件的内容?

[英]How to print contents of a file in c?

我被困在用cygwin上的vim编写的这个c程序的前半部分,在那里我需要将文件的内容打印到标准输出中。 我只想确保我的getNextWord函数正常运行,但是在编译时会收到我将在代码后显示的错误。 到目前为止,这就是我所拥有的。

#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

对于这些错误,我绝对一无所知。 任何帮助将不胜感激。

我认为您的意思是isalnum而不是isalum ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM