【问题标题】:Adding values to char* var from txt file从 txt 文件向 char* var 添加值
【发布时间】:2014-04-04 09:28:18
【问题描述】:

示例文件:

[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);

【问题讨论】:

  • a) 所以你想要一个字符串数组,每行文件一个字符串? b)但它甚至没有编译 - 给我们错误信息。这很重要!
  • 你遇到了什么错误?
  • 是的,我想要一个字符串数组。确切地说,每行一个字符串。错误是因为我没有使用适合我需要的正确 var 类型,所以我认为没有必要显示错误。
  • 你在使用ppos之前没有初始化它们,你没有为buffer分配任何内存,jugadores是一个字符数组而不是字符串数组.

标签: c++ file-io


【解决方案1】:

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

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;
}

【讨论】:

  • 你如何准确地显示这条线?请出示该代码。
  • for (int i=0; i
  • 在您的原始代码中,sizeof(jugadores) 始终为 4 或 8(取决于 32 位或 64 位编译),因为它是指向动态分配数组而不是静态数组的指针。您必须跟踪在数组中放入了多少行。此外,jugadores[i] 是单个char,而不是char*,因此使用%s 是错误的。在我给你的 C 代码中,在循环计数器中使用 numlineslines[i] 将与 %s 一起正常工作。在我给你的 C++ 代码中,在循环计数器中使用 lines.size()lines[i].c_str() 将与 %s 一起使用,或者使用 std::cout 而不是 printf()
猜你喜欢
  • 1970-01-01
  • 2021-02-08
  • 2021-03-25
  • 2021-04-24
  • 2020-02-05
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多