从 txt 文件向 char* var 添加值

Adding values to char* var from txt file

本文关键字:var 添加 char txt 文件      更新时间:2023-10-16

示例文件:

[16bpp] Ponete el cinturon *-*
arfield
Nothing (cumpleanios):
Alkon^
~~|Tampon)
[16bpp] Chico Tonto.
Budin
16bpp] Leooooooooo!!!!!
Ev
16bpp] fedee
etamod
:) mAnKeAno

我希望每个名称位于不同的数组位置...

我尝试了以下代码:

int c;
FILE *file;
file = fopen("bots.txt", "r");
if (file){
    char *buffer;
    char *jugadores = new char[1000];
    int p;
    int pos;
    while ((c = getc(file)) != EOF){
        if (c == 'n'){
            strcpy(jugadores[p], buffer);
            p++;
            buffer = "";
            pos = 0;
        } else {
            buffer[pos] = c;
            pos++;
        }
    }
    fclose(file);
}

但它甚至不会编译...

在 php 中,正确的代码是这样的:

$data = file_get_contents("file.txt");
$names = explode("n", $data);

你的代码有几个缺陷。 你需要更多类似的东西:

FILE *file = fopen("bots.txt", "r");
if (file)
{
    char** lines = new char*[1000];
    int maxlines = 1000;
    int numlines = 0;
    char *buffer = new char[1024];
    int maxbuf = 1024;
    int buflen = 0;
    char *line;
    int c;
    while ((c = getc(file)) != EOF)
    {
        if (c == 'n')
        {
            if (numlines == maxlines)
            {
                char** tmplines = new char*[maxlines+1000];
                memcpy(tmplines, lines, sizeof(char*)*maxlines);
                delete[] lines;
                lines = tmplines;
                maxlines += 1000;
            }
            line = new char[buflen+1];
            memcpy(line, buffer, sizeof(char)*buflen);
            line[buflen] = 0;
            lines[numlines] = line
            numlines++;
            buflen = 0;
        }
        else
        {
            if (buflen == maxbuf)
            {
                char* tmpbuf = new char[maxbuf+1024];
                memcpy(tmpbuf, buffer, sizeof(char)*maxbuf);
                delete[] buffer;
                buffer = tmpbuf;
                maxbuf += 1024;
            }
            buffer[buflen] = c;
            buflen++;
        }
    }
    fclose(file);
    if (buflen > 0)
    {
        if (numlines == maxlines)
        {
            char** tmplines = new char*[maxlines+1000];
            memcpy(tmplines, lines, sizeof(char*)*maxlines);
            delete[] lines;
            lines = tmplines;
            maxlines += 1000;
        }
        line = new char[buflen+1];
        memcpy(line, buffer, sizeof(char)*buflen);
        line[buflen] = 0;
        lines[numlines] = line
        numlines++;
    }
    delete[] buffer;
    // use lines up to numlines elements as needed...
    for (int i = 0; i < numlines; i++)
        printf("%sn", lines[i]);
    for (int i = 0; i < numlines; ++i)
        delete[] lines[i];
    delete[] lines; 
}

话虽如此,既然你正在使用C++,你应该使用C++类来帮助你管理一切。 尝试更多类似的东西:

#include <fstream> 
#include <ostream> 
#include <string>
#include <vector>
std::ifstream file("bots.txt");
if (file.is_open())
{
    std::string line;
    std::vector<std::string> lines;
    while (std::getline(file, line))
        lines.push_back(line);
    file.close();
    // use lines as needed...
    for (int i = 0; i < lines.size(); i++)
        std::cout << lines[i] << std::endl;
}