將一個txt文檔中所有字符讀入一個數組中的代碼

FILE* f = fopen("filename", "rb");
        // get file length
        fseek(f, SEEK_END, 0);
        const int len = ftell(f);
        fseek(f, SEEK_SET, 0);

        // read file
        char* buf = new char[len + 1];
        fread(buf, 1, len, f);
        fclose(f);

        buf[len] = 0;

        // remove /r and /n
        char* buf_1 = buf;
        char* buf_2 = buf;
        while (*buf_1 != '/0') {
                if (*buf_1 != '/r' && *buf_1 != '/n')
                        *(buf_2++) = *buf_1;
                buf_1 ++;
        } // while
        *buf_2 = '/0';
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章