LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2004, 01:39 PM   #1
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Rep: Reputation: 47
simple c question (getline)


I have a line-oriented file-format, and use get line to retrieve the data. However, the (string) data can not have the newline character. I am trying to write a function that doesn't return a string with the newline, but I'm having trouble:

Code:
char * getlineNoEndl(FILE *file)
{
  char *str = NULL;
  int strsize = 0;

  getline(&str, &strsize, file);

  if (strsize >= 2)
  {
    /* shrink string */
    str = realloc(str, (--strsize) * sizeof(char));
    str[strsize - 1] = '\0';
  }
  else
  {
    free(str);
    str = malloc(sizeof(char));
    *str = '\0';
  }
 
  return str;
}
The function currently appears to still return a string with a newline in it.
 
Old 08-10-2004, 02:51 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Different operating systems use different End Of Line markers. An easy way would be to do something like this:
Code:
int i;

for(i = 0;i < strsize;++i)
  if(!isprint(str[i]))
    break;
str[i] = '\0';
The \r or \n at the end of the line will cause isprint() to return 0 and break the loop. Then you can null-terminate the string at that point. If you're concerned about realloc()'ing the string you can use strsize-i to figure out how much to shrink str by.
 
Old 08-10-2004, 05:53 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I think the reason the original wasn't working is that the second argument to getline gets filled in with the amount of space *allocated* to the pointer in the first argument, not the actual number of characters read. You should be using the return value of getline to find the position of the newline instead.

I *believe* that fopen without the "b" modifier to the mode will convert whatever the system's newline character is to \n, so you don't have to worry about different newlines. I'm not sure if I'm remembering right though.
 
Old 08-18-2004, 03:02 PM   #4
aaa
LQ Guru
 
Registered: Jul 2003
Location: VA
Distribution: Slack 10.1
Posts: 2,194

Original Poster
Rep: Reputation: 47
Got it working with this:
Code:
char * getlineNoEndl(FILE *file)
{
  char *str = NULL;
  int strsize = 0;

  strsize = getline(&str, &strsize, file);
  
  if (strsize < 0)
    return NULL;
 
  if (strsize >= 1)
  {
    /* shrink string */
    str = realloc(str, (strsize) * sizeof(char));
    str[strsize - 1] = '\0';
  }
  else
  {
    free(str);
    str = malloc(sizeof(char));
    *str = '\0';
  }
  return str;
}
However, I have encountered another problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ubuntu Fluxbox simple question, simple answer? generallimptoes Linux - Software 3 09-26-2005 02:03 PM
getline hylke Programming 9 06-03-2004 01:24 PM
Installing Programs - A simple question from my simple mind jmp875 Linux - Newbie 6 02-18-2004 09:03 PM
C++ | cin.getline question... Mega Man X Programming 7 01-08-2004 10:34 AM
simple question seeking simple answer enzo250gto Linux - Newbie 1 10-27-2001 04:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration