【发布时间】:2016-02-16 03:04:31
【问题描述】:
所以我遇到了这个问题,我已经尝试解决了大约 8 个小时......我已经放弃了在没有帮助的情况下寻找答案。我试过分别使用realloc() 和malloc(),所以任何输入都会很棒!
在 C 中的目的是允许创建“地图”,稍后我将使用 ncurses 来创建地图。
来自文件的输入如下
10X16 de4 dw9 ds8 g8,7 m3,4 h6,5 p2,2
6X20 dn5 ds4 W4,3 e2,12 M1,1
10X13 ds3 dw9
10X12
5X4
6x12
代码如下:
char *importLevel()
{
FILE *fPointer;
fPointer = fopen("Level", "r"); //Opens text file to read
char* rooms[150];// set up for memory allocation
char commands[150];// set up for pulling data from read file
while (!feof(fPointer))
{
fgets(commands,150, fPointer); // this takes each line from the file
}
*rooms = (char *) malloc(150 * sizeof(char)); // memory allocation
for (int i = 0; i < 150; i++)
{
if (rooms[i] != NULL)
{
*rooms[i] = commands[i]; // supposed to give rooms the string
}
}
fclose(fPointer);// close file
return *rooms; // return pointer
}
我希望我没有现在感觉的那么愚蠢!谢谢:)
编辑:我当时觉得自己很愚蠢
【问题讨论】:
-
你的代码是废话。所以首先,你需要解释你想要在这段代码中做什么。
-
对不起@BLUEPIXY 我是新来的
-
您似乎也是 C 的新手。您需要提供 很多 更多上下文,例如……这段代码应该做什么?你输入的格式是什么?你为什么用C来解决这个问题?等
-
我非常愿意,我将在代码的每个部分添加 cmets,以便您了解它们的用途
-
在 c 中的目的是允许创建“地图”,稍后我将使用 ncurses 来创建地图。
标签: c compiler-errors