【发布时间】: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 类型,所以我认为没有必要显示错误。
-
你在使用
p或pos之前没有初始化它们,你没有为buffer分配任何内存,jugadores是一个字符数组而不是字符串数组.