簡體   English   中英

在C中從文本文件讀取數據

[英]Reading data from text file in C

我有一個文本文件,其中包含用空格分隔的單詞。 我想從文件中提取每個單詞並將其存儲。 所以我打開了文件,但不確定如何將單詞分配給char。

FILE *fp;
fp = fopen("file.txt", "r");
//then i want
char one = the first word in the file
char two = the second word in the file

您不能將一個單詞分配給char。 您可以將一個字符分配給char-從而命名。 您可以將一個單詞分配給一個字符數組 -例如s [128]。

例如:

     char word[128];
     fscanf(fp, "%s", word);

注意,在生產代碼中,您不能僅使用靜態大小的緩沖區,這將導致緩沖區溢出可利用代碼。

您不能在char變量中保留單詞,它必須是字符串或可以擴大的char指針。

嘗試這個;

字符p [10]; //假設一個單詞最多可以包含10個字符。

文件* fp;

fp = fopen(“ file.txt”,“ r”);

的fscanf(fp的, “%s” 時,P);

如果您想更靈活一些-例如:通過選擇識別單詞的字符-您可以查看以下內容:

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

// adjust size for your needs
#define MAX_WORD_LEN 1000

static char *parseable_characters_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy0123456789-";
static char parseable_characters_tbl[256] = {0}; // lookup index table, stores yes/no -> allowed/not allowed

/*
* builds the lookup table
*/
void build_lookup_index(char *table, const char *str)
{
    int i;

    // init table to zero
    memset(table,0,256);

    // set value to 1 at ASCII-code offset of the array if the character is allowed to be
    // part of the word
    for (i=0; str[i]; i++)
        table[(unsigned char)str[i]] = 1;
}

/*
* returns unparsed bytes (kind of offset for next reading operation)
*/
int parse_buffer(char *buf, int size, const char *lookup_table)
{
    int i,l,s;
    char word[MAX_WORD_LEN+1];

    i = 0;
    l = 0;
    s = 0;

    while (i<size) {

        // character not in lookup table -> delimiter
        if (!lookup_table[buf[i]] || !buf[i]) {
            if (l >= MAX_WORD_LEN) {
                fprintf(stderr,"word exceeds bounds\n");
            }
            else if (l > 0) { // if word has at least 1 character...

                // append string-terminator
                word[l] = '\0';
                printf("word found (%d): '%s'\n",l,word);

            }

            // reset word length
            l = 0;

            // save last word offset
            s = i+1;
        }
        else {

            // prevent buffer overflows
            if (l < MAX_WORD_LEN)
                word[l] = buf[i];

            l++;
        }

        if (!buf[i])
            break;

        i++;
    }

    if (s > 0 && size-s > 0) {

        // move rest of the buffer to the start for next iteration step
        memmove(buf,buf+s,size-s);

        return size-s;
    }

    return 0;
}

int main(int argc, char *argv[])
{
    FILE *fh;
    char buf[1000]; // read buffer

    // "rb" because of Windows - we want all characters to be read
    fh = fopen("out.txt","rb");

    // initialize word index
    build_lookup_index(parseable_characters_tbl,parseable_characters_str);

    if (fh) {
        int r,off = 0;
        while (!feof(fh)) {
            r = fread(buf+off,1,sizeof(buf)-off,fh);
            off = parse_buffer(buf,r,parseable_characters_tbl);
        }
        fclose(fh);
    }

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM