Appending << and >> to a char *


 
Thread Tools Search this Thread
Top Forums Programming Appending << and >> to a char *
# 1  
Old 11-21-2012
Appending << and >> to a char *

I am writing a C++ function so I can pass a name and message

I will call as

Code:
pr_error(stream,3,"name","message");

hoping to get

Code:
<<name>>
message

However I am running into problems with my function shown below when trying to append ">>".

Code:
void pr_error (
  FILE*  stream,
  int    shift,
  const char*  name,
  const char*  msg
) {

  using namespace std;
  char*  s;

  s = const_cast<char *>(name);

  //strcat (s, ">>");

  //  strcpy (s, name);

  const char* cc = s;
//  strcat (name,">>");
  cout<<"Hello World"<< endl;
  const char*  format = "%*s\e[0;31m%s\e[0m\n";
//  fprintf (stream, format, shift, " ", s);
  fprintf (stream, format, shift, " ", msg);

}

# 2  
Old 11-21-2012
change the format specification to add << >> and let fprintf do the formatting:

Code:
const char*  format = "<<%*s>>\e[0;31m%s\e[0m\n";

I do not see the need for the escape sequences, especially if the output of the program gets redirected to a log. For example, you may get some crud on the screen users cannot read easily using an editor or whatever. In other words it presupposes (assumes) too much.
# 3  
Old 11-21-2012
I thought to put some color especially when errors occur. Is there a solution to the problem or the only way is to remove the escape sequences. Is there a way to know if the user is redirecting to a file?
# 4  
Old 11-21-2012
Yes, the isatty system call. It needs plain file descriptors, not FILE *, not iostream. STDOUT_FILENO is defined as 1. STDIN and STDERR are 0 and 2 respectively. Things like ls and grep use isatty to only print color output or columnar output to an interactive terminal.

Code:
#include <unistd.h>

int main(void)
{
        if(isatty(STDOUT_FILENO))
                fprintf(stderr, "stdout is a terminal\n");
        else  fprintf(stderr, "stdout is a file or non-terminal device\n");

        return(0);
}

# 5  
Old 11-22-2012
ls by default print columns of filenames, when the output is a pipe that behavior goes away.

When you make a general purpose program that can interface with pipes, redirection, etc. --- you normally call fstat on STDOUT_FILENO and isatty if you like, all of this to see if the destination is a pipe or whatever. isatty() is a convenience call, IMO.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

2. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

3. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

4. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

5. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

6. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

7. Shell Programming and Scripting

appending

hi, a1.txt ------ xyz zxc xya xdf a2.txt ------ a1 a2 b1 b2 cat a1.txt >> a2.txt -->its appending at the end (6 Replies)
Discussion started by: mohan705
6 Replies

8. Programming

char *p and char p[].

Can anyone please explain me the difference between char *p and char p ? Thanks in Advance, Arun. (4 Replies)
Discussion started by: arunviswanath
4 Replies

9. Programming

\n char in C

how can i change line in the file while using write(desc,&c,sizeof(char)); ? (1 Reply)
Discussion started by: C|[anti-trust]
1 Replies

10. Shell Programming and Scripting

appending

i've been fiddling around with append and I know how to append a word into a file, but how would you go about appending a word to a filename? (5 Replies)
Discussion started by: codewarrior7
5 Replies
Login or Register to Ask a Question