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.