- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 c 程序中使用 getline() 函数从文件中读取行,将读取的信息放入结构中,创建一个以该结构作为参数的线程,然后重复。但是,getline() 在 EOF 之前返回 -1,在提供的文件中“id: test2”下的第二组参数之后。
由于 gdb 中的 print errno
返回 0,我假设没有错误,并且 getline() 不知何故似乎认为它已经达到了 EOF。
创建线程部分已被注释掉,因为它与手头的问题无关。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/time.h>
#define BUFFER_SIZE 256
time_t cur;
char* keypath = "home/chy/.ssh/id_rsa";
int logfreq = 1;
int hashfreq = 180;
struct getArgs
{
const char* id;
const char* hostaddr;
uint16_t port;
const char* uname;
const char* path;
};
int main(int argc, char **argv)
{
int th_max = 5;
int th_count = 0;
pthread_t* ths = (pthread_t*) malloc(th_max * sizeof(pthread_t));
FILE* fp;
fp = fopen("config.txt", "r");
char* line = NULL;
char buf[128];
int r = 0;
size_t len = 0;
ssize_t read;
system("mkdir logs && chmod -R a+r logs");
while(1)
{
pthread_t th;
//Get ID
while((read = getline(&line, &l, fp)) != -1)
{
if(!(read > 0))
continue;
if(sscanf(line, "id: %[^\n]\n", buf) == 1)
break;
if(sscanf(line, "keypath: %[^\n]\n", buf) == 1)
keypath = strdup(buf);
else if(sscanf(line, "logfreq: %d\n", &r) == 1)
logfreq = r;
else if(sscanf(line, "hashfreq: %d\n", &r) == 1)
hashfreq = r;
}
if(read == -1)
break;
struct getArgs* args = (struct getArgs*)malloc(sizeof(struct getArgs));
args->id = strdup(buf);
//Get Host Address
if(getline(&line, &len, fp) == -1)
{
free_args(args);
break;
}
if(sscanf(line, "hostaddr: %[^\n]\n", buf) != 1)
{
free_args(args);
continue;
}
args->hostaddr = strdup(buf);
//Get Port Number
if(getline(&line, &len, fp) == -1)
{
free_args(args);
break;
}
if(sscanf(line, "port: %d\n", &r) != 1)
{
free_args(args);
continue;
}
args->port = r;
//Get Username
if(getline(&line, &len, fp) == -1)
{
free_args(args);
break;
}
if(sscanf(line, "username: %[^\n]\n", buf) != 1)
{
free_args(args);
continue;
}
args->uname = strdup(buf);
//def Path
if(getline(&line, &len, fp) == -1)
{
free_args(args);
break;
}
if(sscanf(line, "path: %[^\n]\n", buf) != 1)
{
free_args(args);
continue;
}
args->path = strdup(buf);
// int err = pthread_create(&th, NULL, &getFiles, args);
if(th_count > th_max)
{
th_max *= 2;
ths = (pthread_t*)realloc(ths, th_max * sizeof(pthread_t));
}
*(ths+th_count * sizeof(pthread_t)) = th;
th_count++;
}
fclose(fp);
while(1);
}
文件:
keypath: /home/username/.ssh/id_rsaNOPASSWORD
id: test1
hostaddr: XXX.XXX.XXX.XXX
port: 22
username: hpc
path: /home/hpc/
id: test2
hostaddr: XXX.XXX.XXX.XXX
port: 22
username: hpc
path: /home/hpc/
id: test3
hostaddr: XXX.XXX.XXX.XXX
port: 22
username: hpc
path: /home/hpc/
id: test4
hostaddr: XXX.XXX.XXX.XXX
port: 3844
username: uname
path: /home/uname/hpc/
最后一次getline()前的文件指针内容:
{_flags = -72539000,
_IO_read_ptr = 0x7ffff7ff80d0 " \nid: test3\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test4\nhostaddr: XXX.XXX.XXX.XXX\nport: 3844\nusername: uname\npath: /home/uname/hpc/\n\n",
_IO_read_end = 0x401d10 "H\211l$\330L\211d$\340H\215-\277\006 ",
_IO_read_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_ptr = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_end = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_buf_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"..., _IO_buf_end = 0x7ffff7ff9000 "P\220\377\367\377\177", _IO_save_base = 0x0, _IO_backup_base = 0x0, _IO_save_end = 0x0, _markers = 0x0, _chain = 0x7ffff7bbb880, _fileno = 7, _flags2 = 0, _old_offset = 0, _cur_column = 0,
_vtable_offset = 0 '\000', _shortbuf = "", _lock = 0x603120, _offset = -1, __pad1 = 0x0, __pad2 = 0x603130, __pad3 = 0x0, __pad4 = 0x0, __pad5 = 0, _mode = -1, _unused2 = '\000' <repeats 19 times>}
之后:
{_flags = -72538984,
_IO_read_ptr = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_read_end = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_read_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_ptr = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_write_end = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"...,
_IO_buf_base = 0x7ffff7ff8000 "keypath: /home/username/.ssh/id_rsaNOPASSWORD\n \nid: test1\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /home/hpc/\n \nid: test2\nhostaddr: XXX.XXX.XXX.XXX\nport: 22\nusername: hpc\npath: /ho"..., _IO_buf_end = 0x7ffff7ff9000 "P\220\377\367\377\177", _IO_save_base = 0x0, _IO_backup_base = 0x0, _IO_save_end = 0x0, _markers = 0x0, _chain = 0x7ffff7bbb880, _fileno = 7, _flags2 = 0, _old_offset = 0, _cur_column = 0,
_vtable_offset = 0 '\000', _shortbuf = "", _lock = 0x603120, _offset = -1, __pad1 = 0x0, __pad2 = 0x603130, __pad3 = 0x0, __pad4 = 0x0, __pad5 = 0, _mode = -1, _unused2 = '\000' <repeats 19 times>}
我也尝试用 fgets() 而不是 getline() 实现相同的结果,但结果相同。
是什么导致了这个问题?
编辑:
我已经在同一个文件上运行了代码,除了没有多余的空格/空行以及切换顺序以查看是否有任何影响,但结果是相同的。
我还可以确认 test1 和 test2 的结构已正确填充。
{id = 0x6032e0 "test1", hostaddr = 0x603300 "XXX.XXX.XXX.XXX", port = 22, uname = 0x603320 "hpc", path = 0x603340 "/home/hpc/"}
{id = 0x603390 "test2", hostaddr = 0x6033b0 "XXX.XXX.XXX.XXX", port = 22, uname = 0x6033d0 "hpc", path = 0x6033f0 "/home/hpc/"}
编辑:代码和文件的版本更改
最佳答案
除了评论中讨论的 unsigned/signed type
不匹配之外,您遇到的主要问题是测试中使用的逻辑冲突,以及多个松散的逻辑组织对 getline
和 sscanf
的调用,您将在其中测试 if ((read = getline ... || sscanf ...)
其中 getline
或 sscanf
(在输入或匹配失败时)可能会返回 EOF
(-1
),但无法说明是什么问题是。要清理逻辑,您需要读取配置文件,您 (1) 读取该行 - 一次,(2) 解析标记和值,然后 (3) 测试 tag
和 value
并采取所需的操作。
坚持这种方法,并忽略与您的读取问题无关的线程代码,我调试/重新编写了您的输入例程,为您提供了一种以理智的方式处理读取和分离的方法示例。这样做时,我将所有设置收集在指向结构的指针数组中(例如 struct getArgs **args;
)。您可能不需要使用 pthread 方案将每个结构收集在一个结构数组中,但出于示例目的,已进行了更改。
我还在下面提供了第二个示例,该示例显示了一种额外的方法,其中包含更多检查以确保您的值在 id、hostaddr、port、username 和 path 组中被读取。第一个简单示例将处理跳过输入文件中的空行:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define NARGS 32
#define TSIZE 16
#define VSIZE 256
char *keypath = "home/chy/.ssh/id_rsa";
int logfreq = 1;
int hashfreq = 180;
struct getArgs {
const char *id;
const char *hostaddr;
uint16_t port;
const char *uname;
const char *path;
};
void *xcalloc (size_t n, size_t s);
void *xrealloc_dp (void *ptr, size_t *n);
int main (int argc, char **argv)
{
struct getArgs **args = NULL;
char *line = NULL;
size_t len = 0;
size_t idx = 0;
size_t nargs = NARGS;
size_t i;
ssize_t read;
FILE *fp = argc > 1 ? fopen (argv[1], "r") : fopen ("config.txt", "r");
if (!fp) { fprintf (stderr, "file open failed.\n"); return 1; }
/* allocate NARGS pointer to struct getArgs */
args = xcalloc (NARGS, sizeof *args);
/* read each line in file */
while ((read = getline (&line, &len, fp)) != -1) {
if (read == 1) continue; /* skip blank lines */
char tag[TSIZE] = {0};
char val[VSIZE] = {0};
/* separate tag and value (val) */
if (sscanf (line, "%s %[^\n]%*c", tag, val) != 2) {
fprintf (stderr, "error: sscanf conversion failed.\n");
break;
}
/* handle keypath, logfreq, hashfreq */
if (strcmp (tag, "keypath:") == 0) {
keypath = strdup (val);
continue;
}
if (strcmp (tag, "logfreq:") == 0) {
logfreq = (int)strtol (val, NULL, 10);
continue;
}
if (strcmp (tag, "hashfreq:") == 0) {
hashfreq = (int)strtol (val, NULL, 10);
continue;
}
/* allocate space for args[idx] if "id:", then
handle id, hostaddr, port, username, path */
if (strcmp (tag, "id:") == 0) {
args[idx] = calloc (1, sizeof **args);
args[idx]->id = strdup (val);
continue;
}
if (strcmp (tag, "hostaddr:") == 0) {
args[idx]->hostaddr = strdup (val);
continue;
}
if (strcmp (tag, "port:") == 0) {
args[idx]->port = (uint16_t)strtoul (val, NULL, 10);
continue;
}
if (strcmp (tag, "username:") == 0) {
args[idx]->uname = strdup (val);
continue;
}
/* increment idx on path */
if (strcmp (tag, "path:") == 0)
args[idx++]->path = strdup (val);
if (idx == nargs) /* check idx, realloc */
args = xrealloc_dp (args, &nargs);
}
fclose (fp);
printf ("\n keypath : %s\n logfreq : %d\n hashfreq : %d\n",
keypath, logfreq, hashfreq);
for (i = 0; i < idx; i++)
printf ("\n id : %s\n hostaddr : %s\n port : %hu\n"
" username : %s\n path : %s\n", args[i]->id, args[i]->hostaddr,
args[i]->port, args[i]->uname, args[i]->path);
for (i = 0; i < idx; i++)
free (args[i]);
free (args);
return 0;
}
/* calloc with error check, exit on failure */
void *xcalloc (size_t n, size_t s)
{
register void *memptr = calloc (n, s);
if (memptr == 0) {
fprintf (stderr, "xcalloc() error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
return memptr;
}
/* reallocate memory for a double-pointer from 'n' to 2 * 'n'
* returns pointer to reallocated block on success, exit on
* failure
*/
void *xrealloc_dp (void *ptr, size_t *n)
{
void **p = ptr;
void *tmp = realloc (p, 2 * *n * sizeof tmp);
if (!tmp) {
fprintf (stderr, "xrealloc_dp() error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
p = tmp;
memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
*n *= 2;
return p;
}
xcalloc
和 xrealloc_dp
函数只是对 calloc
和 realloc
进行错误检查的函数(对于双-pointer) 保持代码主体和逻辑清晰。我在以下测试输入上运行代码并收到以下输出:
输入测试文件(带空行)
$ cat ../dat/idhostaddrport.txt
keypath: /home/username/.ssh/id_rsaNOPASSWORD
logfreq: 2
hashfreq: 250
id: test1
hostaddr: XXX.XXX.XXX.XXX
port: 221
username: hpc1
path: /home/hpc1/
id: test2
hostaddr: XXX.XXX.XXX.XXX
port: 222
username: hpc2
path: /home/hpc2/
id: test3
hostaddr: XXX.XXX.XXX.XXX
port: 223
username: hpc3
path: /home/hpc3/
id: test4
hostaddr: XXX.XXX.XXX.XXX
port: 3844
username: uname
path: /home/uname/hpc/
输出
$ ./bin/getline_sscanf_dbg ../dat/idhostaddrport.txt
keypath : /home/username/.ssh/id_rsaNOPASSWORD
logfreq : 2
hashfreq : 250
id : test1
hostaddr : XXX.XXX.XXX.XXX
port : 221
username : hpc1
path : /home/hpc1/
id : test2
hostaddr : XXX.XXX.XXX.XXX
port : 222
username : hpc2
path : /home/hpc2/
id : test3
hostaddr : XXX.XXX.XXX.XXX
port : 223
username : hpc3
path : /home/hpc3/
id : test4
hostaddr : XXX.XXX.XXX.XXX
port : 3844
username : uname
您应该努力的一件事是进一步验证您的输入的方法。确保如果您读取 id
,您也会通过 path
读取每个 id
的剩余值 hostaddr
。添加最少检查的一个简单变体是对读取循环的以下更改:
/* read each line in file */
while ((read = getline (&line, &len, fp)) != -1) {
if (read == 1) continue; /* skip blank lines */
char tag[TSIZE] = {0};
char val[VSIZE] = {0};
/* separate tag and value (val) */
if (sscanf (line, "%s %[^\n]%*c", tag, val) != 2) {
fprintf (stderr, "error: sscanf conversion failed.\n");
break;
}
/* handle keypath, logfreq, hashfreq */
if (strcmp (tag, "keypath:") == 0) {
keypath = strdup (val);
continue;
}
if (strcmp (tag, "logfreq:") == 0) {
logfreq = (int)strtol (val, NULL, 10);
continue;
}
if (strcmp (tag, "hashfreq:") == 0) {
hashfreq = (int)strtol (val, NULL, 10);
continue;
}
/* allocate space for args[idx] if "id:", then
handle id, hostaddr, port, username, path */
if (strcmp (tag, "id:") == 0) {
args[idx] = calloc (1, sizeof **args);
args[idx]->id = strdup (val);
size_t tagseq = 0;
while ((read = getline (&line, &len, fp)) != -1) {
if (read == 1) continue; /* skip blank lines */
/* separate tag and value (val) */
if (sscanf (line, "%s %[^\n]%*c", tag, val) != 2) {
fprintf (stderr, "error: sscanf conversion failed.\n");
break;
}
if (strcmp (tag, "hostaddr:") == 0) {
if (tagseq != 0) {
fprintf (stderr, "error: tagseq failed for hostaddr.\n");
exit (EXIT_FAILURE);
}
args[idx]->hostaddr = strdup (val);
tagseq++;
continue;
}
if (strcmp (tag, "port:") == 0) {
if (tagseq != 1) {
fprintf (stderr, "error: tagseq failed for port.\n");
exit (EXIT_FAILURE);
}
args[idx]->port = (uint16_t)strtoul (val, NULL, 10);
tagseq++;
continue;
}
if (strcmp (tag, "username:") == 0) {
if (tagseq != 2) {
fprintf (stderr, "error: tagseq failed for username.\n");
exit (EXIT_FAILURE);
}
args[idx]->uname = strdup (val);
tagseq++;
continue;
}
/* increment idx on path */
if (strcmp (tag, "path:") == 0) {
if (tagseq != 3) {
fprintf (stderr, "error: tagseq failed for path.\n");
exit (EXIT_FAILURE);
}
args[idx++]->path = strdup (val);
break;
}
}
}
if (idx == nargs) /* check idx, realloc */
args = xrealloc_dp (args, &nargs);
}
fclose (fp);
看看两者,如果您有任何其他问题,请告诉我。
关于C getline() 在 EOF 之前返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749471/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!