Tag: getline

codeblocks和C未定义的getline引用

我试图在C中使用带有代码块的getline,但我无法让它在我的机器上运行。 代码也适用于我也可以访问的服务器,但是我的wifi访问受限,所以我需要这个才能在我的机器上运行。 我使用gcc编译器运行Windows 8.1 64位和13.12代码块。 这是使用getline的三个代码段之一,删除了一些额外的变量。 #include #include // For error exit() #include char *cmd_buffer = NULL; size_t cmd_buffer_len = 0, bytes_read = 0; size_t words_read; // number of items read by sscanf call bytes_read = getline(&cmd_buffer, &cmd_buffer_len, stdin); if (bytes_read == -1) { done = 1; // Hit end of file } 错误非常简单: undefined […]

尽管导入stdio,但getline未在此范围内声明

我需要在C中使用getline() ,但是当我写: #define _GNU_SOURCE #include #include #include int main(int argc, char** argv) { char *line; getline(&line, NULL, stdin); free(line); return (0); } 编译器写入error: getline was not declared in this scope我该怎么办? getdline是不是在stdio.h被推迟了? 我之前从未遇到过这种问题。 我使用GCC GNU编译器。

c getline跳过空白行

while(getline (&line, &line_size, f) != -1){} 我正在使用此function读取线条。 但我想知道我什么时候读一个空行。 有人可以帮忙吗?

getline()函数如何在这里工作?

我不明白函数getline是如何在这里工作的。 为什么换行字符被排除在for循环之外?为什么它在单独的块中测试换行符的存在? #include #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print the longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char […]

保持前导零

我试图从/ proc // maps读取内存地址,我使用以下代码 for (ptr = NULL; getline(&ptr, &n, file) > 0;) { if (ptr[0]== ‘ ‘) { continue; } sscanf(ptr, “%lx-%lx”, &r0, &r1); printf(“r0: %lx, r1: %lx\n”, r0, r1); } 假设文件指向/ proc // maps&ptr是行指针。 但是当你考虑一个map文件时,它不能正确读取文件。 它会降低零,它不会选择零。 所以考虑: 00110000-00123000 r-xp 00000000 08:01 129925 /lib/i686/cmov/libnsl-2.11.1.so 完成我的程序后: r0: 110000, r1: 123000 我如何保持前导零输出这样的东西: r0: 00110000, r1: 00123000 […]

编写getline()实现 – Valgrind错误

我必须重新getline()函数的实现,但是使用文件的文件描述符而不是FILE * 。 我只允许使用malloc()和free() ,以及最多25行的5个函数。 我想我已经正确完成了项目,虽然我是C语言的初学者,但我的代码可能并不好。 当我运行它,它工作正常,但valgrind显示我定义definetely lost x bytes ,x取决于文件长度和READ_SIZE(标头中定义的宏)。 根据valgrind的–leak-check=full ,当我使用malloc dest时,我在str_realloc_cat函数中有内存泄漏。 我试过但找不到我应该在哪里/做其他事情? 以下是我的代码: char *get_next_line(const int fd) { static char *remaining = “”; char *buffer; ssize_t cread; size_t i; i = 0; if (remaining == NULL) return (NULL); if ((buffer = malloc(SOF(char) * READ_SIZE + 1)) == NULL || (cread = read(fd, […]

符合ANSI C的实现是否可以在其标准库中包含其他function?

是否符合ANSI C标准的实现允许在其标准库中包含其他类型和function,除了标准列举的那些之外? (理想的答案将参考ANSI标准的相关部分。) 我特别要求,因为Mac OS 10.7在stdio.h中声明了getline函数,即使使用-ansi标志使用gcc或clang进行编译也是如此。 这打破了几个定义自己的getline函数的旧程序。 这是Mac OS 10.7的错吗? (Mac OS 10.7上getline的手册页说getline符合2008年推出的POSIX.1标准。) 编辑:为了澄清,我发现奇怪的是,在Mac OS 10.7上的ANSI C89程序中包含stdio.h也会getline函数的声明,因为getline不是K&R中枚举的函数之一(可能是ANSI) stdio.h的描述。 特别是,尝试编译noweb : gcc -ansi -pedantic -c -o notangle.o notangle.c In file included from notangle.nw:28: getline.h:4: error: conflicting types for ‘getline’ /usr/include/stdio.h:449: error: previous declaration of ‘getline’ was here 它是否是Mac OS 10.7中的一个错误,即使在编译ANSI C89标准时也包含stdio.h中的getline声明?

c中对’getline’的未定义引用

我正在学习在C编程中使用getline,并尝试了http://crasseux.com/books/ctutorial/getline.html中的代码 #include #include #include int main(int atgc, char *argv[]) { int bytes_read = 1; int nbytes = 10; char *my_string; my_string = (char *)malloc(nbytes+1); puts(“Please enter a line of text”); bytes_read = getline(&my_string, &nbytes, stdin); if (bytes_read == -1) { puts (“ERROR!”); } else { puts (“You typed:”); puts (my_string); } return 0; } 但是,问题是编译器不断返回错误: […]