Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: c code - spilt string lines for menu

  1. #1

    Default c code - spilt string lines for menu

    I want to create a menu in c. Can I convert this bash format into c code? Without doing printf on every line.

    Code:
    printf "
    Scan Virus
    help commands
    -------------
    virus scan linux files
        p1: -l or --linux
        p2: u   freshclam update      (optional)
        p2: l   low system priority   (optional)
        p2: h   high system priority  (optional)
        p2: s   suspend on end        (optional)
        p2: p   shutdown on end       (optional)
        p2: f   scan folder           (optional)
    \n"
    Internet searches have come up empty.

    Thanks.

    FYI, I need a new challenge. So, I'v decided to convert the bash code into c.

  2. #2

    Default Re: c code - spilt string lines for menu

    Untested, but something like this should work:
    Code:
    printf(
    "Scan Virus\n"
    "help commands\n"
    "-------------\n"
    "virus scan linux files\n"
    "    p1: -l or --linux\n"
    "    p2: u   freshclam update      (optional)\n"
    "    p2: l   low system priority   (optional)\n"
    "    p2: h   high system priority  (optional)\n"
    "    p2: s   suspend on end        (optional)\n"
    "    p2: p   shutdown on end       (optional)\n"
    "    p2: f   scan folder           (optional)\n"
    "\n"
    );
    To explain: If you have several strings ("xyz") one after another (only separated by whitespace), they get concatenated and result in one long(er) string. (this concatenation doesn't modify the strings, so you need to add linefeeds explicitly)
    Also, C treats newlines as whitespace, so it is possible to split one command about several lines just like this.

    I.e. the above would be equivalent to this:
    Code:
    printf("Scan Virus\nhelp commands\n-------------\nvirus scan linux files\n    p1: -l or --linux\n    p2: u freshclam update (optional)\n    p2: l low system priority (optional)\n    p2: h high system priority (optional)\n    p2: s suspend on end (optional)\n    p2: p shutdown on end (optional)\n    p2: f scan folder (optional)\n\n");

  3. #3

    Default Re: c code - spilt string lines for menu

    PS: as you only want to print constant lines of text (and not e.g. formatted numbers), you could also use puts() instead of printf(), which is smaller.

  4. #4

    Default AW: Re: c code - spilt string lines for menu

    Quote Originally Posted by wolfi323 View Post
    PS: as you only want to print constant lines of text (and not e.g. formatted numbers), you could also use puts() instead of printf(), which is smaller.
    There is a (small) difference though:
    puts() automatically adds a newline after printing the text, while printf() does not.
    So you'd have to omit the last \n when using puts().
    Last edited by wolfi323; 02-Jul-2020 at 07:17.

  5. #5

    Default Re: AW: Re: c code - spilt string lines for menu

    Quote Originally Posted by wolfi323 View Post
    There is a (small) difference though:
    puts() automatically adds a newline after printing the text, while printf() does not.
    So you'd have to omit the last \n when using puts().
    I tried something like that and it didn't work. Maybe I missed something. Thanks.

    This did work. Thanks to a few hours of web searching.


    Code:
              printf("\
    Scan Virus\n\
    help commands\n\
    -------------\n\
    virus scan linux files\n\
        p1: -l or --linux\n\
        p2: u   freshclam update      (optional)\n\
        p2: l   low system priority   (optional)\n\
        p2: h   high system priority  (optional)\n\
        p2: s   suspend on end        (optional)\n\
        p2: p   shutdown on end       (optional)\n\
        p2: f   scan folder           (optional)\n\
    \n");
    Can you show me an example of 'puts('?

  6. #6

    Default Re: c code - spilt string lines for menu

    why not introduce the options (psx: into an array element? So then you only have to iterate over it to create the string and print it? In that way you can re use those values in other parts of your program avoiding duplicated code

  7. #7

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by mike.flw View Post
    why not introduce the options (psx: into an array element? So then you only have to iterate over it to create the string and print it? In that way you can re use those values in other parts of your program avoiding duplicated code
    The menu called as a function. The menu lines aren't used outside the menu print.

    Split each line of the menu into array elements?

  8. #8

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by lord_valarian View Post
    The menu called as a function. The menu lines aren't used outside the menu print.

    Split each line of the menu into array elements?
    That would be another nice option

  9. #9

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by lord_valarian View Post
    I tried something like that and it didn't work. Maybe I missed something. Thanks.
    Then you probably forgot some of the quotes.

    I did try to compile it afterwards, and it does work as intended.

    This did work. Thanks to a few hours of web searching.
    Yes, a \ at the end of a line means that the line ending is being ignored.
    Disadvantage: you cannot indent the following lines (as the spaces/tabs would be part of the string), so the source code may become a mess.

    Can you show me an example of 'puts('?
    It's pretty much the same, just puts instead of printf (and the last newline is removed because puts() adds it anyway):
    Code:
    puts(
    "Scan Virus\n"
    "help commands\n"
    "-------------\n"
    "virus scan linux files\n"
    "    p1: -l or --linux\n"
    "    p2: u   freshclam update      (optional)\n"
    "    p2: l   low system priority   (optional)\n"
    "    p2: h   high system priority  (optional)\n"
    "    p2: s   suspend on end        (optional)\n"
    "    p2: p   shutdown on end       (optional)\n"
    "    p2: f   scan folder           (optional)\n"
    );

  10. #10

    Default Re: c code - spilt string lines for menu

    How do you use this with a file stream? I need to write out the config file.

    fprintf,fputs

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •