Like Tree1Likes
  • 1 Post By anduril462

Formatted output %*s

This is a discussion on Formatted output %*s within the C Programming forums, part of the General Programming Boards category; Hello, I am going through an example for a class and ran into something that is not working for me ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    Formatted output %*s

    Hello,

    I am going through an example for a class and ran into something that is not working for me the same way as it did for the professor when he demonstrated it.

    He use:
    Code:
    printf(%*s %10ld\n", -maxlen,file,buf.st_size);
    Its a part of an "ls" program, and this is for formatting the data output. maxlen is the number of characters in the name of the longest named file or directory.

    On his it aligns neatly to the left, but when I compile and execute it on mine it does not provide formatting for the string. File is a String pointer to the filename and de is a structure that holds metadata about the file or directory.


    Id like to know what I could be doing wrong as I like the idea of being able to format the column width with a variable instead of hard coded. Thanks for any help.
    Last edited by dtow1; 12-15-2012 at 11:02 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    I would help if you could provide a sample of the output you expect to get and the output you are actually getting. I know, generally, how you want things to line up, but a concrete example would really help, since this issue is about the nuances of printf format strings. You can use code tags to get fixed-width font so it looks correct. By the way, use a forward slash (/) to terminate your code tags, not a backslash (\). Also, please tell us what implementation you are using, i.e. OS, compiler and which C run-time library, if you know. It may be a peculiarity of the implementation you're using.

    EDIT: Oh, and lastly, you don't have variables filename or de, you have file and buf. Are you sure this is correct? What you're telling us doesn't jive with the code you posted. Just want to be sure.
    dtow1 likes this.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Hey, thank you for replying. Sorry to have wasted your time. I'm not sure why it worked when he did it but I've figured out what was happening. It was determining the maxlength for the current name, then printing, and going to the next. So the maxlen variable was changing with each iteration that had a larger value. I modified the example code to loop through once to determine the maxlen value, then to go through again applying the printf formatting and its all lining up nicely now. I am not sure why it worked when he did it, but It appears that the error was a dtow1 error. Thank you for replying though.

    I use Ubuntu 12.04 I believe, and gcc.
    Last edited by dtow1; 12-15-2012 at 11:02 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    No worries, not a waste of time at all. Glad you figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little question about formatted output
    By Extropian in forum C Programming
    Replies: 10
    Last Post: 08-19-2005, 08:30 AM
  2. Formatted output question...
    By Lateralus in forum C Programming
    Replies: 3
    Last Post: 06-15-2005, 10:58 AM
  3. formatted output
    By Magica in forum C Programming
    Replies: 3
    Last Post: 05-11-2003, 11:36 PM
  4. formatted file output
    By ronin in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2002, 09:38 AM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21