从txt文件向char * var添加值

编程入门 行业动态 更新时间:2024-10-23 09:28:57
本文介绍了从txt文件向char * var添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

范例档案:

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

我想要每个名称在不同的阵列位置...

I want each name on a different array position...

我试过这个代码:

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

但它甚至不编译...

But it doesn't even compile...

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

In php the right code would be something like this:

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

推荐答案

您的代码有几个缺陷。你需要更像这样的东西:

Your code has several flaws in it. You need something more like this instead:

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 ++类将帮助管理一切为你。尝试更像这样:

With that said, since you are using C++, you should use C++ classes that will help manage everything for you. Try something more like this instead:

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

更多推荐

从txt文件向char * var添加值

本文发布于:2023-10-28 23:27:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   txt   var   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!