NO

Author Topic: Problem using getline functions  (Read 10481 times)

kraj

  • Guest
Problem using getline functions
« on: October 16, 2012, 04:21:40 AM »
I am using PellesC 7.00.355.
I have included stdio.h, as required by the getline function.
I am running some sample code, as follows:
Code: [Select]
  char * line = NULL;
  size_t len = 0;
  ssize_t read;
  while ((read = getline(&line, &len, stdin)) != -1) {
    printf("Retrieved line of length %zu :\n", read);
    printf("%s", line);
  }
  if (line)
    free(line);
When building, I am getting the following errors:
Quote
Building main.obj.
D:\Program Files\PellesC\Projects\TrapHandle\traphandle\main.c(40): error #2048: Undeclared identifier 'ssize_t'.
D:\Program Files\PellesC\Projects\TrapHandle\traphandle\main.c(40): error #2001: Syntax error: expected ';' but found 'read'.
D:\Program Files\PellesC\Projects\TrapHandle\traphandle\main.c(40): error #2048: Undeclared identifier 'read'.
D:\Program Files\PellesC\Projects\TrapHandle\traphandle\main.c(43): warning #2018: Undeclared function 'getline'; assuming 'extern' returning 'int'.
*** Error code: 1 ***
Done.
After looking at stdio.h, I cahnged the definition from ssize_t to _ssize_t, at which time it compiled, but gave me a warning:
Quote
Building main.obj.
D:\Program Files\PellesC\Projects\TrapHandle\traphandle\main.c(43): warning #2018: Undeclared function 'getline'; assuming 'extern' returning 'int'.
Building traphandle.exe.
Done.
« Last Edit: October 16, 2012, 05:59:18 PM by Stefan Pendl »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Problem using getline functions
« Reply #1 on: October 16, 2012, 06:59:59 AM »
Well, not sure to tell by just the snippet you posted (as that is not a compilable program!)

Otherwise, what's up with that ssize_t stuff?
The type in stdio.h is defined as size_t, just as you are using it to declare len.

And free() is declared in stdlib.h, not in stdioh.h

Beside that, I still get that strange error about getline() not defined, will have to see about that one after I had something for dinner...

Ralf

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Problem using getline functions
« Reply #2 on: October 16, 2012, 12:24:11 PM »
Code: [Select]
#define __STDC_WANT_LIB_EXT2__ 1
#include <stdio.h>
...
May the source be with you

CommonTater

  • Guest
Re: Problem using getline functions
« Reply #3 on: October 16, 2012, 03:55:26 PM »
Here's a working example using getline to fetch characters from the keyboard...
Code: [Select]
// example using getline
#define __STDC_WANT_LIB_EXT2__ 1
#include <stdio.h>
#include <stdlib.h>

int main (void)
  {
    ssize_t chars;
    char* line = calloc(10,sizeof(char));
    size_t size = 10;
 
    chars = getline(&line, &size, stdin);
 
    printf("You typed :\n%s\n",line);
    printf("%d chars, buffer is %d chars\n\n",chars, size);
 
    free(line);

    return 0;
  }
(Note that line cannot be NULL and the carriage return (\n) is stored in the string before the null terminator)
« Last Edit: October 16, 2012, 04:13:25 PM by CommonTater »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Problem using getline functions
« Reply #4 on: October 16, 2012, 05:46:10 PM »

(Note that line cannot be NULL and ...)
It can
Quote
Setting *lineptr to a null pointer and *n to zero are allowed and a recommended way to start parsing a file.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html
May the source be with you

CommonTater

  • Guest
Re: Problem using getline functions
« Reply #5 on: October 16, 2012, 08:21:17 PM »

(Note that line cannot be NULL and ...)
It can
Quote
Setting *lineptr to a null pointer and *n to zero are allowed and a recommended way to start parsing a file.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html

I got exceptions when I tried it Timo... Maybe a Pelles C specific thing?

The Pelles C help file doesn't say anything about it.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Problem using getline functions
« Reply #6 on: October 16, 2012, 09:07:53 PM »
I get the sample to compile bar that strange getline() error during compile...  :-\

I also found out that the sample that the OP posted is from a GCC (*ix) specific example that can be found on SourceForge.

Just haven't gotten a clue as to why the getline() call doesn't compile as used, the declaritions in the include file is there but surrounded by a plethora of #if...

Might try to simplify the expression to narrow this down, but I am all day so far busy chasing after clients who owe me money...  >:(

Ralf

CommonTater

  • Guest
Re: Problem using getline functions
« Reply #7 on: October 16, 2012, 09:26:34 PM »
I get the sample to compile bar that strange getline() error during compile...  :-\

I also found out that the sample that the OP posted is from a GCC (*ix) specific example that can be found on SourceForge.

Just haven't gotten a clue as to why the getline() call doesn't compile as used, the declaritions in the include file is there but surrounded by a plethora of #if...

Might try to simplify the expression to narrow this down, but I am all day so far busy chasing after clients who owe me money...  >:(

Ralf

Hi Ralf... 
I tested it this morning, with my coffee... the example I posted does work.  You have to enable the extensions to make it work, hense the first #define in my sample.  (Thanks for that to Timo, by the way)

What seems puzzling is that most texts recommend starting with the line buffer at NULL... but I got a memory access exception when I did that.  I'm going to guess that's an implementation specific thing.  Pelles help file says it will expand the buffer... It doesn't say it will make the first allocation.

And yes pulling GCC examples off unix boards is probably not the best way to learn Windows C.

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Problem using getline functions
« Reply #8 on: October 16, 2012, 10:02:26 PM »
How about initializing to zero?

Code: [Select]
char * line = {0};
---
Stefan

Proud member of the UltraDefrag Development Team

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Problem using getline functions
« Reply #9 on: October 17, 2012, 05:51:28 AM »
Modified CT's example that works with 32/64-bit:
Code: [Select]
// example using getline
#define __STDC_WANT_LIB_EXT2__ 1
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int chars;
//char *line = calloc(10, sizeof(char));
//unsigned int size = 10;
char *line = NULL;
size_t size = 0;

chars = getline(&line, &size, stdin);

printf("You typed :\n%s\n", line);
printf("%d chars, buffer is %d chars\n\n", chars, size);

free(line);

return 0;
}
TR 24731-2:
http://www.open-std.org/jtc1/sc22/wg14/www/projects
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1337.pdf
« Last Edit: October 17, 2012, 06:08:20 AM by timovjl »
May the source be with you

CommonTater

  • Guest
Re: Problem using getline functions
« Reply #10 on: October 17, 2012, 06:14:13 AM »
Modified CT's example that works with 32/64-bit:

HMMMM.... Now I have to wonder what the heck was going on this morning?

Perhaps I should wait till after my morning coffed to write code samples... :D