how to do this using string array

Hello,

i wan to do c++ program in ubuntu that able to write a summary on the directory example is

drwx-x John 4096
d-w--x John 14
lrwx-x John 2300

How to i use array to put per column into array then access them so i can generate a report so it will be like this

Summary
Total directories = 2 ( the number of d in front determine it)
total link files = 1 ( the number of l in front determine it)
Name of user = John
Total size = 6410 (the total size of last column)

for now is i using char to determine the directories and link file

int main()
{
char input[100]
int directory = 0;

while(cin>>input)
{
if(input[0] == 'd')
directory++;
}
cout<<dir<<endl;
}

this is what i done to find the directories and link, but i cant use char method to grab name and calculate total size...anyone can tell me any method to generate the report...ty for all help
This is probably the sloppiest code I've ever written, I am sure this code can be written in much more elegant manner. I wrote it in 10 min just to give you some idea as to what you have to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<cstring>

using namespace std;

int main() {

char* str[3][1] = {{"dwr--x-x John 2030"},{"d--x-x John 1000"},{"lwr-x-x John 200"}};
char *p;
int dir_count = 0;
int link_count = 0;
char name[50];
int total_size = 0;

for (int i=0,j=0;i<3;i++) {

char getval[100];
strcpy (getval,str[i][j]);
p = strtok (getval," ");

int flag = 0;
while (p!=NULL ) {

   if (flag == 0) {
   char temp;
   temp = *p;
   switch (temp) {
   case 'd':
        dir_count++;
        break;
   case 'l':
         link_count++;
         break;

    }
   }

   else if (flag == 1) {

        strcpy(name, p);      

   }
   else if (flag == 2) {
          total_size += atoi(p);      
   }
   p = strtok(NULL, " "); 
   flag ++;
}
}

cout<<"Total Directories:"<<dir_count<<endl;
cout<<"Total Linked file:"<<link_count<<endl;
cout<<"User Name:"<<name<<endl;
cout<<"Total Size:"<<total_size<<endl;

return 0;

 }


Hope this helps !
ty for the help kevinchkin

I noticed your code is the string stored into array then do the manipulation, is it possible to get the string from input itself?
Last edited on
Of course,
1
2
char str[100]; 
cin.getline(str); 


after this just replace all the brackets for str from the previous code.
Last edited on
so in the end, we not using the 2d array and replace it with

1
2
char str[100]; 
cin.getline(str); 
Well ya, if you want to input the string then you don't need a 2-d array. You can just compute everything on the fly.

If your going to use cin.getline(), make sure you specify a maximum limit. Otherwise your going to create a cascading buffer-overflow through your application. Especially as you have used functions such as strcpy() that do no bounds-checking.

Have a read through:
http://www.cplusplus.com/forum/articles/6046/
It'll give you a good idea of how to read information from streams (files, memory or stdin).
kevin,i wanna ask you, for these piece of code , it could only read the first letter of its flag becos of the string token, may i know wat shld i do if i want to read the second letter or other letter of that flag .

1
2
3
4
5
6
7
8
9
10
11
12
 if (flag == 0) {
   char temp;
   temp = *p;
   switch (temp) {
   case 'd':
        dir_count++;
        break;
   case 'l':
         link_count++;
         break;

    }

Last edited on
Hey! Sorry for not replying for so long. Anyways if you'll just use
p instead of temp = *p
(Of course, considering you are not assigning this value to temp)
You'll get entire string. And if you want to access other characters in that string then just do p[index]
So, the code can be rewritten as:

1
2
3
4
5
6
7
8
9
10
11
int (flag ==0) { 

switch (p[0]) { 

case 'd': 
    dir_count++; 
    break; 
case 'l': 
    link_count++; 
    break; 
}


Now p will have entire string, and to access individual characters in the string just use p[index]. Make sure that index is within the string length, otherwise it will return a garbage value, since C does not do array bound checking.
Hope this helps.
Last edited on
Topic archived. No new replies allowed.