Results Of A Variable Into An Array Using C Language

Can C add its results into an array like bash? For example using bash:

cat /etc/passwd
**truncated for space **
gdm:x:109:118:Gnome Display Manager:/var/lib/gdm:/bin/false
mysql:x:110:122:MySQL Server,,,:/nonexistent:/bin/false
statd:x:111:65534::/var/lib/nfs:/bin/false
bigbadwolf:x:1001:1001::/home/bigbadwolf:

contents of array.sh
#!/bin/bash
declare -a filecontent=(`cat "/etc/passwd"`)
echo ${filecontent[3]}|gawk -F ':' '{print $1}'
sudo ./array.sh
bigbadwolf

I began diving into the most basic of C:

cat /etc/passwd
**truncated for space **
gdm:x:109:118:Gnome Display Manager:/var/lib/gdm:/bin/false
mysql:x:110:122:MySQL Server,,,:/nonexistent:/bin/false
statd:x:111:65534::/var/lib/nfs:/bin/false
bigbadwolf:x:1001:1001::/home/bigbadwolf:

and in my adventure in doing so , I came up with this primitive little program:

    #include <stdio.h>
    #include "saint.h"
    #include "error.h"
    #include "string.h"
    #define BUFLEN 256
    char buffer[BUFLEN];
    int main() {
    FILE *fp;
    char *results;
                fp = fopen("/etc/passwd", "r");
                while(fgets(buffer, BUFLEN, fp)) {
                        if (results=strstr(buffer, "bigbadwolf" )) {
                           printf("Here are the goodies:\n%s", results);
                                        }
                        }
                fclose(fp);
        }

which simply works by passing the results of strstr --> results:

bigbadwolf:x:1001:1001::/home/bigbadwolf:

I tried to get fancy thinking that it could be easily done via attempting to declare variable and array in a one-liner but to no avail:

    #include <stdio.h>
    #include "saint.h"
    #include "error.h"
    #include "string.h"
    #define BUFLEN 256
    char buffer[BUFLEN];
    int main() {
    FILE *fp;
    //char *results[];
    //char arr[15];
    //char results;
    //results = &arr;
                fp = fopen("/etc/passwd", "r");
                while(fgets(buffer, BUFLEN, fp)) {
                        if (char *results[]=strstr(buffer, "bigbadwolf" )) {
                          printf("Here are the goodies:\n%s", results[0]);
                                        }
                        }
                fclose(fp);
        }
sudo gcc read2.c -o read
read2.c: In function �main':
read2.c:15:8: error: expected expression before �char'
    if (char *results[]=strstr(buffer, "bigbadwolf" )) {
        ^
read2.c:16:43: error: �results' undeclared (first use in this function)
       printf("Here are the goodies:\n%s", results[0]); 
                                           ^
read2.c:16:43: note: each undeclared identifier is reported only once for each function it appears in

How do I get bigbadwolf by itself in stdout?

bigbadwolf

Take me to the promised land please.

C does not change what variables do depending on their type, or do anything for you when you ask it to convert one type to another. At best, it will ball it up and cram it through the mail slot, i.e. take you completely literally rather than doing the extra work you wanted.

So, if you want the string to be split, you're going to have to tell it to split it.

strtok is fairly good at doing that. It breaks strings apart on a list of letters, any letter in the list is considered a separator. (I add \n to remove newlines from the end while its at it.)

Two caveats:

  1. It modifies buf . For example, the line a:b:c:d:e\n would become a\0b\0c\0d\0e\0 . So you can't call it on anything you can't modify, i.e. strtok("a:b:c:d:e", ":"); would crash.
  2. It returns pointers to things inside buf. Every time you overwrite buf[], i.e. every time you call fgets(), all the pointers strtok() gave you last time will point anywhere or nowhere.
int main()
{
        char buf[512];
        FILE *fp=fopen("/etc/passwd", "r");

        while(fgets(fp, 512, stdin) != NULL)
        {
                char *tok[64], ntok=1;

                tok[0]=strtok(buf, ":\n");
                // strtok() will keep returning more strings until
                // it runs out and returns NULL.
                tok[ntok]=strtok(NULL, ":\n"); 

                while(tok[ntok] != NULL) tok[++ntok]=strtok(NULL, ":\n");

                if(strcmp(tok[0], "bigbadwolf") == 0) puts(tok[0]);
        }
        fclose(fp);
}
2 Likes

Corona corrected your problems. Consider a more generalized approach.
This is not production code. It could use more error checking, for example. I use it for quick and dirty stuff.

It breaks a file into an array of lines based on the \n character.
You could have it break on spaces or punctuation or whatever, in order to emulate creating an array of words in a file. Just change the delimiter in the split call. For large files create a larger result[] array.

You could call split again on a line and split the line by spaces + punctuation to get a word count, for example. You could add a regex call and split on just about anything.

#include "min.h"

#define AVG_LINE_SZ 20

// from M Rochkind
ssize_t readall(int fd, char *buf, size_t bytes)
 {
     ssize_t bytes_read = 0;
     ssize_t n=0;
     do {
         if ((n = read(fd,
                       &buf[bytes_read],
                       bytes - bytes_read)) == -1)
         {
             if (errno == EINTR)  // resume on INTR
             {
                 continue;
             }
             else
             {              
                 return -1;
             }
         }
         if (n == 0)
             return bytes_read;
         bytes_read += n;
     } while (bytes_read < bytes);
     return bytes_read;
 }

char **split(char **r, char *src, const char *delim)
{
   int i=0;
   char *p=strtok(src, delim);
   for( ; p; p=strtok(NULL, delim) )
   {
      r[i++]=p;
      r=NULL;
   }
   return r;
}

size_t filesize(const char *fname)
{
         struct stat st;
         if(stat(fname, &st)== -1 )
         {
            fprintf(stderr, "cannot stat: %s %s\n", fname, strerror(errno));
            exit(1);
         }
         return st.st_size;
}

char *readfile(char *buf, char *fname, size_t len, char **result)
{
   FILE *fp=fopen(fname, "r");
   if(readall(fileno(fp), buf, len)>0)
   {
     split(result, buf, "\n");  // split on newlines     
     fclose(fp);
   }
   else
   {    
     perror("");
     exit(1);
   }
   return buf;

}

int main(int argc, char **argv)
{
   size_t len=(argc>1)? filesize(argv[1]): 0;
   char *result[8192]={NULL};  // <- this a problem for larger files
   int i=0;
   char *buf=(len>0)? malloc(len+2): NULL;
   if(buf==NULL) {perror(""); exit(1);}
   readfile(buf, argv[1], len, result);
   while(result)
     printf("%s\n", result[i++]);
   free(buf);
   return 0;  
}

1 Like

Speaking more generally, C does have a way to slurp an entire file into memory at once: mmap. It's actually better than reading it into memory, a 200MB file won't waste 200MB RAM just sitting there, only the parts you're using will actually be "read". To your program, it's a giant array of bytes corresponding to the entire file.

Since you actually want the file to be organized into lines, etc, this may not be too useful for you.

1 Like

I believe the path I need to go is to use a struct of arrays and pass the data to it.

Since arrays are not arbitrary-sized in C, you would be wasting a lot of space.

Also, just dumping data into the array wouldn't make it line up with the array. You would still need to process the data using something like the code we've already shown you.

1 Like