簡體   English   中英

從txt文件向char * var添加值

[英]Adding values to char* var from txt file

示例文件:

[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("%s\n", 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;
}

暫無
暫無

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

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