- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基本上我正在用 C 语言编写一个基于文本的角色扮演游戏,我想创建一个 map 系统。基本上,我遇到问题的功能是从如下所示的文件中读取“文本映射”:
----------\n
|c x [\n
| x |\n
] |\n
----------\0
它基本上是使用二维数组构建的。 *EDIT 我添加了 map 在实际数组中的样子。是不是因为我不喜欢终止每一行
这是我遇到问题的功能:
char** readMap(char* map_to_read,int h, int w){
FILE* fp;
int a = 0, b = 0;
char map_return[h][w];
char* c;
fp = fopen(map_to_read, "r");
for(a = 0; a < h; a++){
for(b = 0; b < w; b++){
c = (char*)malloc(sizeof(char) * w);
map_return[a][b] = fgets(c, w, fp);
printf("%s", c);
}
free(c);
}
fclose(fp);
return map_return;
}
直到最后一切都正常读取,因为 fgets() 没有读取 EOL。这是 printf 从内部看的样子:http://i.imgur.com/KojbjDm.png
我可以为此再找一双眼睛吗?
最佳答案
What's the value in
w
? For your shown data, it should be at least 12 to get the newline too (10 characters, newline and null). You're going to have problems later because you can't (safely) return the local arraymap_return
, but that's a separate bug. Also, you should be getting type mismatch warnings on themap_return[a][b] = fgets(c, w, fp);
line becausemap_return[a][b]
is achar
andfgets()
returns achar *
. And you can't afford tofree(c)
if you're saving a pointer to it. There are so many problems here...
你回复了:
Basically it's
array[h][w]
, sow
represents the number of elements in one line of the array.
得到了进一步的回应:
So you need two separate chunks of memory. One is used to read the line and validate it. It can be simply
char line[128];
. You then useif (fgets(line, sizeof(line), fp) == 0) { ...process EOF/error...}
. And assuming that passes, you validate the line and when it passes the validation, then you can arrange to copy up tow
characters from the line into themap_return
array. You have to decide whether you are playing with strings (terminated with a '\0') or not. You can make a case for either. You then have to deal with the problem of 'not returning a local variable'.
我建议您更改函数的接口(interface),以便调用者为其分配内存。
此代码编译(但尚未运行)。它不会在读取的行上做太多验证;您可以决定还要做些什么。
#include <stdio.h>
#include <stdbool.h>
extern bool readMap(char* map_to_read, int h, int w, char map[h][w]);
bool readMap(char* map_to_read, int h, int w, char map[h][w])
{
FILE* fp;
if ((fp = fopen(map_to_read, "r")) == 0)
return false;
for (int a = 0; a < h; a++)
{
char line[128];
if (fgets(line, sizeof(line), fp) == 0)
{
fclose(fp);
return false;
}
for (int b = 0; b < w; b++)
{
// Validation
if (line[b] == '\n' || line[b] == '\0')
{
fclose(fp);
return false;
}
map[a][b] = line[b];
printf("%c", line[b]);
}
putchar('\n');
}
fclose(fp);
return true;
}
此代码假设您没有在 map
数组中存储以 null 结尾的字符串。
调用示例:
int h = 5;
int w = 10;
char map[h][w];
if (mapRead("somefile", h, w, map))
...process initialized map...
else
...report failure...
函数的错误报告很少;您可以根据需要对其进行改进。
关于c - fgets 不读 EOL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15737589/
这与这个问题非常相似: What is the differrence between `* text=auto` and `* text eol=lf` in .gitattributes? 但我特
Notepad++(甚至使用其他工具)是否有任何方法可以一次性在多个文件上自动更改行结尾? 即将 Windows EOL (CRLF) 和 UNIX EOL (LF) 文件的混合转换为所有 Windo
我正在尝试根据设置更改字符串的行结尾。基本上我有字符串,大多以 LF 结尾,很少有其他东西,但它会发生,并且我希望能够将它们更改为 CRLF 或 CR(如果被要求),或者确保它们是纯粹的 LF(如果被
当我尝试在我的服务器上上传一个 php 文件时,我收到一条消息:"Parse error: ..." 我知道这是什么意思,但问题是别的。 如果我在本地服务器上编辑文件(我的计算机上安装了 XAMPP)
我正在尝试创建一个输出文件以在类项目中使用,但在扫描此行上的字符串文字时它不断给出 EOL: outfile = open(r'C:\Users\kay\Documents\CCA Classes\C
这是我的代码并收到以下错误消息:第 8 行 sepFile=readFile.read().split('\')SyntaxError:扫描字符串文字时 EOL你可以帮帮我吗?谢谢。 import m
我有一个包含\n EOL 字符的制表符分隔文件,看起来像这样: User Name\tCode\tTrack\tColor\tNote\n\nUser Name2\tCode2\tTrack2\tCo
所以我正在尝试使用这段代码 我收到错误 SyntaxError: EOL while scanning string literal 代码: def clean_tweet(self, tweet):
在路径= bla bla行上,它在扫描字符串文字时显示EOL,即使它是用“”关闭的,我在我的代码中经常发生这种情况,这真的很烦人,因为我永远无法修复它。 import os import shutil
我试图在输入字符串中找到每个“a -> b, c, d”模式。我使用的模式如下: "^[ \t]*(\\w+)[ \t]*->[ \t]*(\\w+)((?:,[ \t]*\\w+)*)$" 这个模式
我正在尝试使用简单的所见即所得编辑器。 JSLint 表示它有“Bad escapeing of EOL”。由于我是 javascript 的新手,我很难弄清楚它的含义,因为我正在使用在线找到的代码。
我正在为 Android 编写一个网络应用程序。根据协议(protocol)规范,每行的结尾必须是 CR/LF 组合。虽然我用自己的字符串生成它没有问题,但有时我必须处理用户输入并转换行尾。 我在内部
这个问题应该由 Oracle 支持人员在技术上回答,但他们的响应时间很慢。因此,我想问一下,是否有版本指南提到哪些版本的 Oracle Weblogic 已停产及其日期? http://www.ora
BufferedReader.readLine() 会自动删除 EOL 字符,我不能简单地执行 readLine(),然后在其末尾添加“\r”。我试过了 InputStream myFile = ne
在扫描字符串文字时,它不断出现EOL,但这是什么意思? 这是它不断调用错误的部分: if health2 <= 3: print ("With all the strength you have
我有这个方法: def get_chunksize(path): """ Breaks a file into chunks and yields the chunk sizes.
这个问题在这里已经有了答案: Remove the newline character in a list read from a file [duplicate] (5 个答案) 关闭 8 年前。
基本上我正在用 C 语言编写一个基于文本的角色扮演游戏,我想创建一个 map 系统。基本上,我遇到问题的功能是从如下所示的文件中读取“文本映射”: ----------\n |c x [\n
我正在尝试从给定的 .txt 或 .csv 平面文件中识别 EOL 字符是什么。根据平面文件中第一行数据的 EOL 字符是什么,我想相应地处理该文件中的数据(我正在使用批量加载在 SQL Server
有人可以帮我修改这些脚本以忽略错误并继续运行吗?我只需要弄清楚如何使脚本跳过这些错误并完成其余的行。 这是完整的 Python 脚本: # Import system modules import s
我是一名优秀的程序员,十分优秀!