C getline()返回EOF之前的-1
我在c程序中使用getline函数从文件中读取行,将读取的信息放入结构,创建一个以该结构为参数的线程,然后重复。 但是,它getline在提供的文件中id:test2下的第二组参数之后,在EOF之前返回-1。 由于gdb中的print errno返回0,所以我假设没有错误,getline似乎认为它已经达到了EOF。 创建线程部分已被注释掉,因为它与当前的问题无关 守则:C getline()返回EOF之前的-1,c,eof,getline,C,Eof,Getline,我在c程序中使用getline函数从文件中读取行,将读取的信息放入结构,创建一个以该结构为参数的线程,然后重复。 但是,它getline在提供的文件中id:test2下的第二组参数之后,在EOF之前返回-1。 由于gdb中的print errno返回0,所以我假设没有错误,getline似乎认为它已经达到了EOF。 创建线程部分已被注释掉,因为它与当前的问题无关 守则: #include <stdio.h> #include <stdlib.h> #include <
#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);
}
最后一个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>}
编辑:
代码和文件的版本更改
sscanf(line, "id: %[^\n]\n", buf) != 1
与空行不匹配,导致读取循环在该点失去同步。随着id:test2被吞并,进一步的问题从那里层叠而来,最终您遇到了EOF
[ Matches a nonempty sequence of characters ...
线路
sscanf(line, "id: %[^\n]\n", buf) != 1
与空行不匹配,导致读取循环在该点失去同步。随着id:test2被吞并,进一步的问题从那里层叠而来,最终您遇到了EOF
[ Matches a nonempty sequence of characters ...
除了注释中讨论的无符号/有符号类型不匹配之外,您遇到的主要问题是测试中使用的冲突逻辑,以及对getline和sscanf的多个组织松散的调用,您将在其中测试read=getline…||sscanf。。。如果getline或sscanf on input或matching failure返回EOF-1,则无法判断问题出在哪里。为了清理逻辑,您需要读取配置文件,其中1读取行-一次,2解析标记和值,然后3测试标记和值,并采取所需的操作
坚持这种方法,忽略与读取问题无关的线程代码,我调试/重新编写了输入例程,为您提供了一个以合理方式进行读取和分离的示例。在此过程中,我收集了指向struct的指针数组中的所有设置,例如struct getArgs**args;。您可能不需要使用pthread方案收集结构数组中的每一个,但出于示例目的,已经进行了更改
我还包括了下面的第二个示例,该示例显示了一种额外的方法,该方法需要进行更多的检查,以确保在id、hostaddr、端口、用户名和路径组中读取您的值。第一个简单示例将处理跳过输入文件中的空行:
#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;
}
输出
你应该努力的一件事是如何进一步验证你的输入。确保如果您读取了一个id,那么您也会通过路径读取每个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);
看看这两个问题,如果您还有其他问题,请告诉我。除了注释中讨论的未签名/签名类型不匹配之外,您遇到的主要问题是测试中使用的冲突逻辑,以及对getline和sscanf的多个松散组织的调用,您将在其中测试read=getline…|sscanf。。。如果getline或sscanf on input或matching failure返回EOF-1,则无法判断问题出在哪里。为了清理逻辑,您需要读取配置文件,其中1读取行-一次,2解析标记和值,然后3测试标记和值,并采取所需的操作
坚持这种方法,忽略与读取问题无关的线程代码,我调试/重新编写了输入例程,为您提供了一个以合理方式进行读取和分离的示例。在此过程中,我收集了指向struct的指针数组中的所有设置,例如struct getArgs**args;。您可能不需要使用pthread方案收集结构数组中的每一个,但出于示例目的,已经进行了更改
我还包括了下面的第二个示例,该示例显示了一种额外的方法,该方法需要进行更多的检查,以确保在id、hostaddr、端口、用户名和路径组中读取您的值。第一个简单示例将处理跳过输入文件中的空行:
#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;
}
输出
你应该努力的一件事是如何进一步验证你的输入。确保如果您读取了一个id,那么您也会通过路径读取每个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);
请查看这两个文件,如果您还有其他问题,请告诉我。谢谢,但我尝试在配置文件上运行相同的代码,没有多余的空格/换行符,结果是相同的。我将在顶部的帖子中对此进行澄清。你说没有多余的空格/换行是什么意思?只要一行中有一个空行或两个换行符,问题就出现了。在调用sscanf.@DavidC.Rankin之前,您可能需要将sscanf移到读取循环中,并测试读取是否大于0,因为没有多余的换行符,我的意思是没有双换行符的实例。为了确定,我按照你的建议做了,结果是
谢谢,但是我尝试在没有多余空格/换行符的配置文件上运行相同的代码,结果是一样的。我将在顶部的帖子中对此进行澄清。你说没有多余的空格/换行是什么意思?只要一行中有一个空行或两个换行符,问题就出现了。在调用sscanf.@DavidC.Rankin之前,您可能需要将sscanf移到读取循环中,并测试读取是否大于0,因为没有多余的换行符,我的意思是没有双换行符的实例。为了确定,我按照你的建议做了,结果是一样的。你的其他变量在哪里声明的?键路径、logfreq等。。是全局的吗?是的,为了清晰起见,我在中编辑了它们。您是否在编译时启用了警告?i、 在编译字符串中使用-Wall-Wextra?我询问的原因是您的sscanf字符串中存在签名/非签名类型不匹配。您的EOF问题很可能来自sscanf,例如,如果在第一次成功转换或匹配失败发生之前到达输入端,则返回EOF。为了确认,将getline和sscanf放在不同if测试中的不同行上进行隔离。我已将代码编辑为用于测试您的建议的版本。刚刚尝试使用Wall和Wextra,相关警告都与将int*传递给uint32\u t*有关。我修复了这些问题并再次运行程序,但结果保持不变。文件:是您正在使用的完整数据文件吗?今晚晚些时候我将有一点时间来完全调试代码,但在调试之前,我想确保我有当前的输入。其余的变量声明在哪里?键路径、logfreq等。。是全局的吗?是的,为了清晰起见,我在中编辑了它们。您是否在编译时启用了警告?i、 在编译字符串中使用-Wall-Wextra?我询问的原因是您的sscanf字符串中存在签名/非签名类型不匹配。您的EOF问题很可能来自sscanf,例如,如果在第一次成功转换或匹配失败发生之前到达输入端,则返回EOF。为了确认,将getline和sscanf放在不同if测试中的不同行上进行隔离。我已将代码编辑为用于测试您的建议的版本。刚刚尝试使用Wall和Wextra,相关警告都与将int*传递给uint32\u t*有关。我修复了这些问题并再次运行程序,但结果保持不变。文件:是您正在使用的完整数据文件吗?今晚晚些时候我将有一点时间来完全调试代码,但在调试之前,我想确保我有当前的输入。
/* 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);