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 09-19-2005, 09:02 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
printf: postion cursor to output columns


hi lq, this works how i want in redhat linux (the first example). but in aix it doesn't work. see below for code/output

Code:
[schneidz@lq file-lst]$ cat columnate.c
#include <stdio.h>
#include <string.h>
 

int main(int argc, char *argv[])
{
 FILE * fstream;
 fstream = fopen(argv[1], "r");
 char buff[255], col1[64], col2[64], col3[64];
 int y = 3;
 int x = 5;
 int c;
 

 c = fscanf(fstream, "%s", &col1);
/*/ while(fgets(buff, 255, fstream) != EOF) /*/
 while(c == 1)
 {
  printf("\e[%d;%d%c%s\n", y, x, 'H', col1);
  x += 20;
  c = fscanf(fstream, "%s", &col2);
  printf("\e[%d;%d%c%s\n", y, x, 'H', col2);
  x += 20;
  c = fscanf(fstream, "%s", &col3);
  printf("\e[%d;%d%c%s\n", y, x, 'H', col3);
  y++;
  x = 5;
  c = fscanf(fstream, "%s", &col1);
 }
 return 0;
}
[schneidz@lq file-lst]$ cat test.txt
row1 l33t h4x0r
row2 hello world
row3 chun-li akuma
[schneidz@lq file-lst]$ columnate.x shh.txt
                                                                                
    row1                l33t                h4x0r
    row2                hello               world
    row3                chun-li             akuma
this is what it does in aix:
Code:
/datasheets> cc  columnate.c -o columnate.x
"columnate.c", line 14.46: 1506-342 (W) "/*" detected in comment.
"columnate.c", line 17.10: 1506-235 (W) Incorrect escape sequence \e. \
ignored.
"columnate.c", line 20.10: 1506-235 (W) Incorrect escape sequence \e. \
ignored.
"columnate.c", line 23.10: 1506-235 (W) Incorrect escape sequence \e. \
ignored.

/datasheets>  columnate.x test.lst
e[3;5Hrow1
e[3;25Hl33t
e[3;45Hh4x0r
e[4;5Hrow2
e[4;25Hhello
e[4;45Hworld
e[5;5Hrow3
e[5;25Hchun-li
e[5;45Hakuma
can someone help me figure out how to handle this escape sequence in aix...
thanks,

Last edited by schneidz; 09-19-2005 at 09:04 PM.
 
Old 09-19-2005, 09:28 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
My guess would be that the AIX c libraries and terminals don't support the use of those escape sequences to position the cursor. They may not support any such escape sequences. You may want to look at ncurses.
 
Old 09-20-2005, 12:47 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Creating colums using printf is easy:
Code:
printf("%-20s %10d\n",text1,int1);
will create a 20 char textfield (left aligned) and a 10 char number field. Replace -20s by 20s if you want it right-aligned text field.

This should work on all platforms.

Please note that if the string is greater than the allocated space, your columns get corrupted.

Last edited by Wim Sturkenboom; 09-20-2005 at 12:49 AM.
 
Old 09-20-2005, 02:25 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
There are several separate issues here:
1. Q: Why doesn't the AIX C compiler recognize "\e"?
A: Because it doesn't - and it doesn't have to. "\e" is an extension.

2. Q: Even if the AIX C compiler recognized the "esc", would the
AIX runtime correctly format my columns?
A: Not necessarily. It would *only* work if your terminal driver
happened to recognize and handle ANSI escape sequences.

In answer to Q1: All you have to do is specify the escape in binary (ascii(27)), instead of using "\e":

"e[3;5H" == "\0x1b[3;5H"

In answer to Q2, the best way to assure consistent formatting (without having to worry about whether or not your user's display supports ANSI escape sequences) is to use the built-in formatting provided by "printf", as Wim suggested.

You can also, as Matir point out, use "curses", which is a layer *abve* ANSI escapes (and, in fact, ANSI escapes are one of many terminal control protocols supported by curses).

Your .. PSM

Last edited by paulsm4; 09-20-2005 at 02:27 AM.
 
Old 09-20-2005, 09:55 AM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,197

Original Poster
Rep: Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642Reputation: 642
thanks, i'll try them out.

good discussion paul/ matir

i needed that example wim. i didn't know how to do column formatting in printf (definately one of those things that are easy, if you know how to do it )

happy hacking,
 
Old 09-20-2005, 09:59 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally posted by schneidz
i needed that example wim. i didn't know how to do column formatting in printf (definately one of those things that are easy, if you know how to do it )
Took me a long time as well.
 
  


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
[Mouse cursor] flicking on animated cursor Creak Debian 1 06-10-2005 03:16 AM
Changing the start postion of ext2/ext3 a thing Linux - Hardware 1 06-07-2005 07:16 PM
Output problems with printf pateldenish Programming 1 02-24-2005 10:02 AM
Removing Columns From Output darthtux Programming 4 08-01-2004 10:21 PM
printf output with commas eastsuse Linux - General 1 03-09-2004 08:26 PM


All times are GMT -5. The time now is 05:01 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration