Q: remove characters from EOL I read a buffer from a serial device. It returns me these results (2 lines every time)
Hello World.
My name is John.

Hello World.^M^JMy name 
is Mike.

Hello World.^M^JMy name 
is ^M^JERROR Peter.

These results are in Linux command line. ^M^J is the EOL and means \r\n in Windows. The first result is ok but the other two are terrible. Is there any way to check for ^M^J characters and remove them? Because I want these results:
Hello World.
My name is John.

Hello World.
My name is Mike.

Hello World.
My name is Peter.

With this code I read the buffer
char buff[150];
memset(buff, 0, sizeof(buff));
for (;;)
{
  n=read(fd,buff,sizeof(buff));
  printf("%s", buff);
}

UPDATE
I open and configure my device in this way
int open_port(void)
{
int fd; // file description for the serial port 
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
 //perror("open_port: Unable to open /dev/ttyAMA0 - ");
 printf("open_port: Unable to open /dev/ttyAMA0. \n");
}
else
{
  fcntl(fd, F_SETFL, 0);
  printf("port is open.\n");
}

return(fd);
} //open_port

And configure port
int configure_port(int fd)      // configure the port
{
 struct termios port_settings;      // structure to store the port settings in
 cfsetispeed(&port_settings, B9600);    // set baud rates
 cfsetospeed(&port_settings, B9600);
 port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
 port_settings.c_cflag &= ~CSTOPB;
 port_settings.c_cflag &= ~CSIZE;
 port_settings.c_cflag |= CS8;
 tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
 return(fd);

} //configure_port


A: Firstly, ^M^J are end of line, not end of file.
Secondly, read reads binary data from the specified file descriptor. It reads the number of characters you have specified till it reaches the end of the file, or gets an error. If you want to read lines at a time, read one byte at a time, or use some other line oriented I/O calls (sscanf, that sort of thing)

A: You can check out this question, which proposes a function for reading lines from a file and dealing with Windows' carriage return.

A: The printf() is behaving in an interesting fashion when it sees a \r\n rather than a lone \n.  It interprets the paired character end-of-line as not an end-of-line and so  rather than doing the usual end-of-line-function, it shows you a ^M^J.  Simple eliminating the \r will then give you the desired behavior.  
  char buff[150];
  int n = read(fd,buff,sizeof(buff));  // buff is not NUL terminated
  if (n < 0) {
    // deal with I/O error
    }
  if (n == 0) {
    // deal with end-of-file
    }
  else {
    for (int i=0; i<n; i++) {
      if (isprint(buff[i]) || (buff[i] == '\n')) {
        putchar(buff[i]);
      }
      else if (buff[i] == '\r') {
        ; // drop it
      }
      else {
        ; // TBD deal with unexpected control codes and codes 127-255
      }
    }
  }


Notes:
1) You filled buff before from the serial device using a read().  As serial devices are binary, the bytes read may include NUL bytes.  Reading an array of bytes with sporadically intersperse NUL bytes in a buffer and treating it like a NUL terminated character string will lead to missed data.
2) A read() does not append a \0 byte to the end of the buffer it reads and may explain your "ERROR".
3) In general, you are reading a binary device and writing to a textual output.  The incoming binary stream is likely ASCII text using \r\n as an end-of-line but your stdout wants to use \n as end-of-line.  As long as the bytes are printable ASCII (codes 32-126), all works as expected when printing to stdout.  But when you read a \0, \r, \n, paired \r\n, other control characters, communication errors, etc., you need to consider how you want that displayed.

A: Open the file with O_TEXT
#include <fcntl.h>
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY | O_TEXT);

