Welcome, Guest. Please login or register.

Author Topic: More elegant way of removing/ignoring \n  (Read 2375 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline MarcbTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2007
  • Posts: 407
    • Show only replies by Marcb
More elegant way of removing/ignoring \n
« on: May 21, 2009, 01:39:21 PM »
I'm putting together a small app that reads paths entered line by line in a text file.

I know the issue with reading a variable length line using Fgets and the newline character is as old as the hills but I was wondering if anyone has a more elegant way of dealing with the newline character than my method? :

Fgets(buf,sizeof(buf),*file);
buf[strlen(buf)-1]='\0';

It works but I always think it's too messy and that there must be a better way...
"...but some animals are more equal than others."
 

Offline MrZammler

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 720
    • Show only replies by MrZammler
Re: More elegant way of removing/ignoring \n
« Reply #1 on: May 21, 2009, 01:49:22 PM »
That's not bad, but to be complete you should also check for DOS line endings (\r\n) in case those files are edited in a Windows machine.
Anyway is the only way
 

Offline MarcbTopic starter

  • Sr. Member
  • ****
  • Join Date: Nov 2007
  • Posts: 407
    • Show only replies by Marcb
Re: More elegant way of removing/ignoring \n
« Reply #2 on: May 21, 2009, 02:00:30 PM »
That's a good point, these days it would be very likely the file would be edited on a Windows machine, thanks for the tip !
"...but some animals are more equal than others."
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: More elegant way of removing/ignoring \n
« Reply #3 on: May 21, 2009, 03:17:32 PM »
Quote from: Marcb;455462
I'm putting together a small app that reads paths entered line by line in a text file.

I know the issue with reading a variable length line using Fgets and the newline character is as old as the hills but I was wondering if anyone has a more elegant way of dealing with the newline character than my method? :

Fgets(buf,sizeof(buf),*file);
buf[strlen(buf)-1]='\0';

It works but I always think it's too messy and that there must be a better way...

One way to handle it is to use sscanf:
Code: [Select]
#include <stdio.h>

int main(void)
{
  char tmp[80], buf[80];
  while (fgets(tmp, sizeof(tmp), stdin))
  {
    if (sscanf(tmp, &quot;%[^\r\n]&quot;, buf) != 1)
    {
      buf[0] = '\0';
    }
    printf(&quot;%s\n&quot;, buf);
  }
  return 0;
}
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: More elegant way of removing/ignoring \n
« Reply #4 on: May 21, 2009, 03:21:41 PM »
Quote from: MrZammler;455463
That's not bad, but to be complete you should also check for DOS line endings (\r\n) in case those files are edited in a Windows machine.

As far as I know all libc implementations on windoze do automagic \r\n -> \n translation when reading file opened in 'text' mode (that is no "b" is appended to the fopen mode. stdin/stdout/stderr are in this mode, too). Also, when writing to filehandle that is in 'text' mode single \n again is translated to \r\n.

Checking for \r doesn't hurt, however.
 

Offline Trev

  • Hero Member
  • *****
  • Join Date: May 2003
  • Posts: 1550
  • Country: 00
    • Show only replies by Trev
Re: More elegant way of removing/ignoring \n
« Reply #5 on: May 28, 2009, 07:09:18 AM »
It should also be noted that the value of '\n' is entirely library-dependent, and '\n' can and should be converted to an environment-specific newline sequence by the library. Technically, a text stream written on system A can't be safely read on system B until a low-level line ending conversion has taken place. Thankfully, the conversion is typically 0x0D0A <-> 0x0A, but you can't take it for granted, particularly when you know the text stream was created on a non-ASCII source system.

As noted, '\n' in the C standard library translates to CR+LF in the text stream in DOS/Windows and LF in AmigaDOS.

EDIT: If you're looking to do strict ASCII conversion or scanning, then "\x0d\x0a" for "\r\n" and "\x0a" for "\n" might be "safer."

Wikipedia's Newline article covers this in pretty good detail.
« Last Edit: May 28, 2009, 07:16:38 AM by Trev »