I did a program to read the output of the UNIX command ls -ali
and count the number of directories, write files, link files etc.
But there seeems to be something wrong with my code and I cant compile it. I cant seem to find out whats wrong with it.
This program is executed by entering the command ls -ali | ./a.out
So the output of the list is piped into my program.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[255];
int countWrite = 0;
int countLink = 0;
int countDir = 0;
int countSize = 0;
string owners[255];
char* s;
string pid,perm,dir,owner,group,size,date,file;
char d,l,w;
while (cin.getline(input,255,'\n'))
{
s = input ;
[pid,perm,dir,owner,group,size,date,file] = strtok(s," ");
//tokenize into substrings
if (perm == "d*")//count directories
countDir++;
if (perm == "l*")//count link files
countLink++;
if (perm == "*w*")//count writeable files
countWrite++;
for (int i = 0 ; i<255 ; i++)
if (owners[i] != owner)
owners[i] = owner;
size+=size;//count total size of files in bytes
}
return 0;
}#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char input[255];
int countWrite = 0;
int countLink = 0;
int countDir = 0;
int countSize = 0;
string owners[255];
char* s;
string pid,perm,dir,owner,group,size,date,file;
char d,l,w;
while (cin.getline(input,255,'\n'))
{
s = input ;
[pid,perm,dir,owner,group,size,date,file] = strtok(s," ");
//tokenize into substrings
if (perm == "d*")//count directories
countDir++;
if (perm == "l*")//count link files
countLink++;
if (perm == "*w*")//count writeable files
countWrite++;
for (int i = 0 ; i<255 ; i++)
if (owners[i] != owner)
owners[i] = owner;
size+=size;//count total size of files in bytes
}
return 0;
}