Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: c code - spilt string lines for menu

  1. #11

    Default Re: c code - spilt string lines for menu

    why not make it more generic? Instead of write into a file on the code directly, just redirect the output of the script to a file? `command > text.txt`
    So then you will get the following content into a file, either using put or 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)
    Last edited by mike.flw; 08-Jul-2020 at 06:21. Reason: correcting format of the message

  2. #12

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by lord_valarian View Post
    How do you use this with a file stream? I need to write out the config file.

    fprintf,fputs
    Well, the C syntax won't change depending on which functions you use...

    You'd need to adjust the function calls though, fprintf and fputs take an additional file parameter to depict the stream:
    Code:
    int printf(const char *format, ...);
    int fprintf(FILE *stream, const char *format, ...);
    
    int puts(const char *s);
    int fputs(const char *s, FILE *stream);
    I.e. the above would need to be changed to something like this:
    Code:
    fprintf(file,
    "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"
    );
    or:
    Code:
    fputs(
    "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"
    , file);
    You'd also need to open (and close) the file stream of course.
    Code:
    FILE *fopen(const char *pathname, const char *mode);
    
    int fclose(FILE *stream);
    For more details, I'd suggest to take a look at the man or info pages, e.g. "man fopen":
    Code:
    FOPEN(3)                      Linux Programmer's Manual                      FOPEN(3)
    
    NAME
           fopen, fdopen, freopen - stream open functions
    
    SYNOPSIS
           #include <stdio.h>
    
           FILE *fopen(const char *pathname, const char *mode);
    
           FILE *fdopen(int fd, const char *mode);
    
           FILE *freopen(const char *pathname, const char *mode, FILE *stream);
    
       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
    
           fdopen(): _POSIX_C_SOURCE
    
    DESCRIPTION
           The  fopen()  function  opens  the file whose name is the string pointed to by
           pathname and associates a stream with it.
    
           The argument mode points to a string  beginning  with  one  of  the  following
           sequences (possibly followed by additional characters, as described below):
    
           r      Open  text file for reading.  The stream is positioned at the beginning
                  of the file.
    
           r+     Open for reading and writing.  The stream is positioned at  the  begin-
                  ning of the file.
    
           w      Truncate  file  to  zero  length  or create text file for writing.  The
                  stream is positioned at the beginning of the file.
    
           w+     Open for reading and writing.  The file  is  created  if  it  does  not
                  exist,  otherwise  it  is  truncated.   The stream is positioned at the
                  beginning of the file.
    
           a      Open for appending (writing at end of file).  The file is created if it
                  does not exist.  The stream is positioned at the end of the file.
    
           a+     Open  for  reading and appending (writing at end of file).  The file is
                  created if it does not exist.  The initial file position for reading is
                  at  the beginning of the file, but output is always appended to the end
                  of the file.
    ...
    Last edited by wolfi323; 08-Jul-2020 at 08:02.

  3. #13

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by mike.flw View Post
    That would be another nice option
    Well, I can just add it as commented out code. The menu as a string array. I have lots of null elements for my own learning. 'scanvirus' is well commented and is also meant to help others learn code.

  4. #14

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by mike.flw View Post
    why not make it more generic? Instead of write into a file on the code directly, just redirect the output of the script to a file? `command > text.txt`
    So then you will get the following content into a file, either using put or printf:
    hmm, I done it in bash. Never seen it done in c code. How?

  5. #15

    Default Re: c code - spilt string lines for menu

    During this, i'v been writing scanvirusbin code. I'v finished the permissions code. It now creates the needed files and checkpermisions. File username and groupname is for another topic. The code is written out based on the usual gathering of info. There might be problems with it.

    I can't use fprint with the config file. Since, it includes '%' symbol, more maybe added later.

    Code:
         //create config file if not present
         File_Found= stat(VirusConfig, &file_stats);
         if (File_Found != 0)
         {
              FilePtr=fopen(VirusConfig, "w");
              if (FilePtr != NULL)
              {
    fputs("______________________________scanvirus configuration______________________________\n", FilePtr);
    fputs("date +'%Y-%m-%d %I:%M:%S%P'\n", FilePtr);
    fputs("TimeStamp= %I:%M:%S%P\n", FilePtr);
    fputs("DateStamp= %Y-%m-%d\n", FilePtr);
    fputs("______________________________________________________________________________\n", FilePtr);
    fputs("ExcludedScanFolders= dev etc kdeinit5__0 proc tmp srv sys var .snapshots\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);
    fputs("Bash Suspend Command\n", FilePtr);
    fputs("1= 'systemctl suspend' - openSUSE, Ubuntu, Fedora, Arch, Debian, etc\n", FilePtr);
    fputs("2= 'pm-suspend' - Void, Gentoo, Devuan etc - pm-utils power management suite\n", FilePtr);
    fputs("SuspendCommand= 1\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);
    fputs("Suspend or power-off lock screen - GNOME KDE\n", FilePtr);
    fputs("LockScreenCommand= 0\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);
    fputs("MSWIN scan - partition file system types\n", FilePtr);
    fputs("ScanPartitionFileSystems= ntfs vfat\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);
    fputs("List users group filter\n", FilePtr);
    fputs("UserGroupFilter= users\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);
    fputs("Scan Log Format\n", FilePtr);
    fputs("%o  OS Type\n", FilePtr);
    fputs("%s  Virus Status\n", FilePtr);
    fputs("%f  Scan Folder\n", FilePtr);
    fputs("%n  Scan Time\n", FilePtr);
    fputs("%c  Command Options\n", FilePtr);
    fputs("%t  Time Stamp\n", FilePtr);
    fputs("%d  Date Stamp\n", FilePtr);
    fputs("\n", FilePtr);
    fputs("ScanLogFormat= %o %s %f %n %c %t %d\n", FilePtr);
    fputs("___________________________________________________________________________________\n", FilePtr);                  
             }
              else
              {
                   printf("can't write to file\n");
                   return(-1);
              }
              fclose(FilePtr);
    Can I shorten this code?

  6. #16

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by lord_valarian View Post
    I can't use fprint with the config file. Since, it includes '%' symbol, more maybe added later.
    You can, you'd just need to escape the '%' symbols you want to output, i.e. use '%%'.

    Or, to put it differently, this will print a % character:
    Code:
    printf("%%");
    Can I shorten this code?
    I don't understand that question.

    You are just writing lines to a file.
    The only way to "shorten" that is to omit some of the lines you are writing. You'd need to know yourself whether that would be ok...

    You could write all lines with a single call to fputs() (like your initial question), but that's not really shorter.

    As it is now, you probably should remove the trailing '\n' at the end of each line though.
    As already mentioned, fputs() does print a newline after outputting your text.
    I.e. the code you posted now would generate an empty line between every line of text.
    Last edited by wolfi323; 16-Jul-2020 at 10:17.

  7. #17

    Default Re: c code - spilt string lines for menu

    Quote Originally Posted by wolfi323 View Post
    You can, you'd just need to escape the '%' symbols you want to output, i.e. use '%%'.

    Or, to put it differently, this will print a % character:
    Code:
    printf("%%");

    I don't understand that question.

    You are just writing lines to a file.
    The only way to "shorten" that is to omit some of the lines you are writing. You'd need to know yourself whether that would be ok...

    You could write all lines with a single call to fputs() (like your initial question), but that's not really shorter.

    As it is now, you probably should remove the trailing '\n' at the end of each line though.
    As already mentioned, fputs() does print a newline after outputting your text.
    I.e. the code you posted now would generate an empty line between every line of text.
    I have it working now. Thanks.

    If I remove the newline character, that makes it all stack on one line and parse the spaces into new lines.

    Code:
    fputs(
    "______________________________scanvirus configuration______________________________\n"
    "date +'%Y-%m-%d %I:%M:%S%P'\n"
    "TimeStamp= %I:%M:%S%P\n"
    "DateStamp= %Y-%m-%d\n"
    "______________________________________________________________________________\n"
    "ExcludedScanFolders= dev etc kdeinit5__0 proc tmp srv sys var .snapshots\n"
    "___________________________________________________________________________________\n"
    "Bash Suspend Command\n"
    "1= 'systemctl suspend' - openSUSE, Ubuntu, Fedora, Arch, Debian, etc\n"
    "2= 'pm-suspend' - Void, Gentoo, Devuan etc - pm-utils power management suite\n"
    "SuspendCommand= 1\n"
    "___________________________________________________________________________________\n"
    "Suspend or power-off lock screen - GNOME KDE\n"
    "LockScreenCommand= 0\n"
    "___________________________________________________________________________________\n"
    "MSWIN scan - partition file system types\n"
    "ScanPartitionFileSystems= ntfs vfat\n"
    "___________________________________________________________________________________\n"
    "List users group filter\n"
    "UserGroupFilter= users\n"
    "___________________________________________________________________________________\n"
    "Scan Log Format\n"
    "%o  OS Type\n"
    "%s  Virus Status\n"
    "%f  Scan Folder\n"
    "%n  Scan Time\n"
    "%c  Command Options\n"
    "%t  Time Stamp\n"
    "%d  Date Stamp\n"
    "ScanLogFormat= %o %s %f %n %c %t %d\n"
    "___________________________________________________________________________________\n", FilePtr);





    Now, I need help using getline to split the line into a string array.

  8. #18

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

    Quote Originally Posted by lord_valarian View Post
    If I remove the newline character, that makes it all stack on one line and parse the spaces into new lines.

    Code:
    fputs(
    "______________________________scanvirus configuration______________________________\n"
    "date +'%Y-%m-%d %I:%M:%S%P'\n"
    "TimeStamp= %I:%M:%S%P\n"
    "DateStamp= %Y-%m-%d\n"
    ...
    Yes, in this case you do need the "\n" because fputs() only adds a new line after the whole text you pass to it. (how should it know where you'd want to insert newlines anyway?)

    In your previous post, your code called fputs() for every single line though.

    Now, I need help using getline to split the line into a string array.
    getline() only reads a line (i.e. all input until the next newline) though, it doesn't split it.

    Depending on what you want to do exactly, you could also use e.g. fread(), fscanf(), fgetc() or fgets() to read from a file (or stdin).

    You'd need to explain what your goal is though...

  9. #19

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

    Quote Originally Posted by wolfi323 View Post
    Yes, in this case you do need the "\n" because fputs() only adds a new line after the whole text you pass to it. (how should it know where you'd want to insert newlines anyway?)

    In your previous post, your code called fputs() for every single line though.


    getline() only reads a line (i.e. all input until the next newline) though, it doesn't split it.

    Depending on what you want to do exactly, you could also use e.g. fread(), fscanf(), fgetc() or fgets() to read from a file (or stdin).

    You'd need to explain what your goal is though...
    I meant for the next posted message.

    Thanks to all. I have what I need to start the next phase of coding.

Page 2 of 2 FirstFirst 12

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
  •