- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想制作一个函数,从给定的文本文件中读取您选择的一行。继续作为参数的函数(打开的 int fd
和 int line_number
)它必须使用 C 语言和 Unix 系统调用(read
和/或 open
)来执行此操作。它还应该读取任何空格,并且它不能有实际限制(即该行必须能够有您选择的长度)。我做的功能是这样的:
char* read_line(int file, int numero_riga){
char myb[1];
if (numero_riga < 1) {
return NULL;
}
char* myb2 = malloc(sizeof(char)*100);
memset(myb2, 0, sizeof(char));
ssize_t n;
int i = 1;
while (i < numero_riga) {
if((n = read(file, myb, 1)) == -1){
perror("read fail");
exit(EXIT_FAILURE);
}
if (strncmp(myb, "\n", 1) == 0) {
i++;
}else if (n == 0){
return NULL;
}
}
numero_riga++;
int j = 0;
while (i < numero_riga) {
ssize_t n = read(file, myb, 1);
if (strncmp(myb, "\n", 1) == 0) {
i++;
}else if (n == 0){
return myb2;
}else{
myb2[j] = myb[0];
j++;
}
}
return myb2;
}
直到最近,我还认为这行得通,但确实存在一些问题。使用消息队列,read_line
读取的字符串作为空字符串 ("\0") 接收。我知道消息队列不是问题,因为尝试传递普通字符串不会产生问题。如果可能的话,我想要一个修复程序,并解释为什么我应该以某种方式更正它。这是因为如果我不理解我的错误,我就有可能在未来重蹈覆辙。
编辑 1. 根据答案,我决定添加一些问题。我如何结束 myb2?有人可以根据我的代码给我一个例子吗?我如何提前知道构成要读取的一行 txt 的字符数?
编辑 2. 我不知道该行有多少个字符,所以我不知道要分配多少个字符;这就是我使用 *100
的原因。
最佳答案
您在以下位置发生内存泄漏:
char* myb2 = (char*) malloc((sizeof(char*))*100);
memset(myb2, 0, sizeof(char));
if (numero_riga < 1) {
return NULL;
}
在分配内存之前检查 numero_riga
。
下面的循环充其量也是可疑的:
int i = 1;
while (i < numero_riga) {
ssize_t n = read(file, myb, 1);
if (strncmp(myb, "\n", 1) == 0) {
i++;
}else if (n == 0){
return NULL;
}
}
您没有检查 read()
是否真的足够快地返回任何内容,当您检查时,您(再次)泄漏内存并忽略任何事先读取的内容,并且您没有检测到错误( n < 0
)。当您确实检测到换行符时,只需将 1 添加到 i
即可。您决不会将读取的字符保存在缓冲区中(例如 myb2
)。总而言之,这似乎已经彻底崩溃了......除非......除非你试图从头开始读取文件中的第 Nth 行,而不是文件中的下一行文件,这是更常见的。
你需要做的是:
我想我可能会像这样使用函数 get_ch()
:
static inline int get_ch(int fd)
{
char c;
if (read(fd, &c, 1) == 1)
return (unsigned char)c;
return EOF;
}
然后在主要的 char *read_nth_line(int fd, int line_no)
函数中你可以做:
char *read_nth_line(int fd, int line_no)
{
if (line_no <= 0)
return NULL;
/* Skip preceding lines */
for (int i = 1; i < line_no; i++)
{
int c;
while ((c = get_ch(fd)) != '\n')
{
if (c == EOF)
return NULL;
}
}
/* Capture next line */
size_t max_len = 8;
size_t act_len = 0;
char *buffer = malloc(8);
int c;
while ((c = get_ch(fd)) != EOF && c != '\n')
{
if (act_len + 2 >= max_len)
{
size_t new_len = max_len * 2;
char *new_buf = realloc(buffer, new_len);
if (new_buf == 0)
{
free(buffer);
return NULL;
}
buffer = new_buf;
max_len = new_len;
}
buffer[act_len++] = c;
}
if (c == '\n')
buffer[act_len++] = c;
buffer[act_len] = '\0';
return buffer;
}
添加测试代码:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
extern char *read_nth_line(int fd, int line_no);
…code from main answer…
int main(void)
{
char *line;
while ((line = read_nth_line(0, 3)) != NULL)
{
printf("[[%s]]\n", line);
free(line);
}
return 0;
}
这从标准输入中每隔三行读取一次。它似乎工作正常。最好对边界条件(短线等)进行更详尽的检查,以确保它不会滥用内存。 (测试长度为 1 的行——仅换行符——最多 18 个字符和 valgrind
表明它是可以的。随机更长的测试似乎也是正确的。)
关于c - 在 Unix 上用 C 读取文本文件的一行——我的 read_line 坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893186/
我制作了一个简单的 Chrome 扩展程序,可以自动将我带到我正在观看的系列节目的下一集。只是要清楚:代码有效! :p 但是,由于对 Chrome API 的调用大部分是异步的,我不得不将函数分成 3
在发布此问题之前,我尝试了在 Android 部分找到的所有答案,但均未成功... 由于某种原因,设备中的图像质量很差,而 Eclipse 和虚拟设备中的图像质量非常好 查看屏幕截图示例: examp
我想在一个小型机器集群(尽可能小)上支持大约 10,000 个并发 HTTP 客户端。我想在用户使用应用程序时与每个客户端保持连接,以允许服务器推送更新。 我相信异步 IO 通常被推荐用于这些类型的长
基本上,当我在 Eclipse 上自动完成(按 CTRL+Space)时,程序会滞后大约 5 秒。这真的很烦人,因为我经常使用自动完成功能。我该如何解决这个问题? 我正在处理的工作区位于具有 1Gbi
在我的模式中,我已经规范化了我的数据库并且到处都有 FK,因为社交网络中有如此多的链接关系,尤其是将用户链接到所有内容。 现在很明显,在社交网络中,性能会成败。这意味着“读取”时间比“写入”时间更重要
我有一个名为 globals.swift 的文件 代码很简单,看起来像这样。 import Foundation import CoreData import UIKit var g_workOrde
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
偶然发现了一个计算数字平方根的(糟糕的)算法。陷入了关于时间复杂度的小争论。我断言时间复杂度是 O(n^2) 因为对于 n 个输入,它将被乘以 n 次。我的 friend 断言时间复杂度实际上是 O(
这是一个一般性的问题,但为了解释它,我将使用一个具体的例子。 我有一个加载文档的函数。如果该文档不存在,它将创建它,如果它存在,它将把它转换为一个 JSON 数组。我总是希望此函数返回某种数组,无论
我要找 坏 使用继承的例子。我不是很有创意,所以这是我能想到的最好的: class Car : public Engine {} 汽车有发动机,但它不是发动机。 这可能有助于解释这个概念,但我相信还有
我很好奇......在内存缓存中使用压缩有什么优点/缺点吗? 最佳答案 许多客户端进行压缩,但服务器本身没有进行压缩。 客户端压缩意味着通过网络发送的数据更少,但更重要的是,单个对象更小。较小的物体可
我正在尝试将 OpenGL 渲染封装在渲染器类中。 我很确定我的窗口类工作正常(我检查了错误),但我认为错误在我的 Sprite 类中,但我不知道它是什么。 这是我的 Sprite 类: sprite
max(M,N,M):-M >= N,!. max(M,N,N). 我正在读一本教科书,说陈述性和程序性含义不同...我不知道该怎么做。 有人可以指出我正确的方向吗? 最佳答案 tl; dr:这不是关
我在线性回归模型上使用留一交叉验证。拥有 8869 个观察结果,原因如下: reg = LinearRegression() list_Rs = cross_val_score(reg, X_34_c
我在想....我有一个网站,其中正在进行大量搜索/排序。如果我将每次搜索的结果项 ID 缓存到 MEMORY 表中,然后简单地执行 WHERE item_id IN ("1", "5", "44",
我有一个 adopted为 Silverlight 实现一个简单的(无升级或超时)ReaderWriterLock,我想知道任何具有适当专业知识的人都可以验证它的设计是好是坏。对我来说它看起来很不错,
这是我在单击按钮时删除所选项目的代码,我在此代码中添加了警报对话框,添加此警报框后,发生 fatal error 异常。 public class MycustomAdapter extends B
我正在从 MySQL 数据库中获取数据。结果在对象中返回。 然后我可以像这样访问数据: $db_data->row 在 foreach 循环中,我通过添加一个数组来更改 $db_data,例如: fo
在我完成的几个 .NET C# web 服务项目中,我已经在单例模式的帮助下访问了静态数据库。然后前几天我的 friend 告诉我这是一件坏事,因为如果对同一个数据库实体发出大量请求,那么数据库将因为
在类的构造函数中进行数据库查询以便在创建类的新实例时加载它是否是一种不好的做法? class Home { private $home_id = null; private $home
我是一名优秀的程序员,十分优秀!