| | |
i need to use C++ to format the output from the ls -ali command
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
the ls -ali output will be treated like a string input for the c++ program i need to write.
i have tried to output ls -ali command into a txt file and below is the output of the ls -ali:
-rwxr--r-- 1 User1 root 26 Nov2 19:51 Mydb.txt
basically, the first column is the permissions of the file , like writable permission or read only permission.
and the second column will be the directories and the third column will be the user ...etc.
The problem here is i dun know how to write my c++ problem to sum up how many files are writable permission file and also to show the user name ...etc.
Basically is how c++ read column? Not that i didn't try to write the code, i seriously need some kick start ...i have no idea how to read column!
i have tried to output ls -ali command into a txt file and below is the output of the ls -ali:
-rwxr--r-- 1 User1 root 26 Nov2 19:51 Mydb.txt
basically, the first column is the permissions of the file , like writable permission or read only permission.
and the second column will be the directories and the third column will be the user ...etc.
The problem here is i dun know how to write my c++ problem to sum up how many files are writable permission file and also to show the user name ...etc.
Basically is how c++ read column? Not that i didn't try to write the code, i seriously need some kick start ...i have no idea how to read column!
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
I'm no linux guru, but i think you could redirect the output of ls to a file within a system() call. I forget how to do this in *nix but in windows it would be something like:
dir > file.txt
Then this file can be opened for input and parsed. The only problem with using system calls is portability. But if you're testing linux specific commands then I'm sure windows users won't expect it to run anyway.
Now to actually parse it, I would sugguest making some sort of struct with members that you want to load in, then instead of loading in columns, load them one word at a time (left to right) into the respective struct members (most likely an array of a struct). Since this data will always have the same format (if not tampered with) parsing shouldn't be that difficult to do.
dir > file.txt
Then this file can be opened for input and parsed. The only problem with using system calls is portability. But if you're testing linux specific commands then I'm sure windows users won't expect it to run anyway.
Now to actually parse it, I would sugguest making some sort of struct with members that you want to load in, then instead of loading in columns, load them one word at a time (left to right) into the respective struct members (most likely an array of a struct). Since this data will always have the same format (if not tampered with) parsing shouldn't be that difficult to do.
C++ Syntax (Toggle Plain Text)
struct sFiles { char * _szFilename; char * _szPath; //i forget how many attributes linux filesystems use bool _attrtibuteFlag[4]; //have some more crap here for other stuff you want to load }; int main(); { //this should probably be dynamically allocated... sFiles Files[255]; int i(0); char * tmpIn; //assuming the file fFileIn has been declared and opened for input while (!fFileIn.eof() && !fFileIn.fail() && i < 255) { //I don't actually have any idea which order this stuff is saved //so this probably won't quite work right. i++; fFileIn.getline(tmpIn, 255, '\n'); for (int ii(0); ii < strlen(tmpIn); ++ii) { //parsing code goes here //looking for various flags which //indicate the end of input //ie a space after the path+filename // would indicate the end //of the path/filename. Then the //last \ before the space indicates //the start of the filename only, //with the path preceeding it //i'm not actually testing this code, //and don't have much time...so I'll //leave the hard part up to you (this code) } } }
Last edited by skatamatic; Oct 15th, 2008 at 10:31 pm.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
C++ Syntax (Toggle Plain Text)
struct sFiles { char _szFilename[255]; char _szPath[255]; //i forget how many attributes linux filesystems use bool _attrtibuteFlag[4]; //have some more crap here for other stuff you want to load }; int main(); { //this should probably be dynamically allocated... sFiles Files[255]; int i(0), iStage(0); char * tmpIn; //assuming the file fFileIn has been declared and opened for input while (!fFileIn.eof() && !fFileIn.fail() && i < 255) { //i dont really have any idea what the file would look like in //linux, so this is just a general rough idea of what to do i++; iStage = 0; fFileIn.getline(tmpIn, 255, '\n'); for (int ii(0); ii < strlen(tmpIn); ++ii) { switch (iStage) { case 0: if (tmpIn[i] == ' ') { iStage++ break; } Files[i]._szPath[ii] = tmpIn[i]; break; case 1: //parsing code goes here //looking for various flags which //indicate the end of input //ie a space after the path+filename // would indicate the end //of the path/filename. Then the //last \ before the space indicates //the start of the filename only, //with the path preceeding it //i'm not actually testing this code, //and don't have much time...so I'll //leave the hard part up to you (this code) } } }
There's a more helpful version of the above code, but don't try to copy + paste it, as it won't compile. I kind of just left off in the switch (parsing) statement. The rest is up to you.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
This would be so much easier to parse out using scripting
.
If you are going to parse the output of ls -al, keep in mind that your permissions, are for the root, the group and the user. And the preceeding "d" indicates whether that is a directory or not. So you will need to separate those, and if you are only interested in the user level permissions, save those and drop the rest.
513062 -rwxrwxrwx+ 1 smith dev 10876 May 16 9:42 part2
Your first column is the i-node for this file, the column indicates that you have a file where the user, group and other permissions are rwx respectively. The next indicates column the # of files in that directory. the next 2 columns indicate the user and the group that own this file. The next is the total number of bytes in the file, then the timestamp when the file was last modified, followed by the file name.
Lets say you redirect your ls -al contents to a file (ls -al > contents). You can then open your contents file and read in the values into a list of some sort. Each object in the list may have the following elements. The only values you will really need to parse out are the permissions and perhaps the timestamp. The rest of it is pretty standard and you should just be able to read it in.
.If you are going to parse the output of ls -al, keep in mind that your permissions, are for the root, the group and the user. And the preceeding "d" indicates whether that is a directory or not. So you will need to separate those, and if you are only interested in the user level permissions, save those and drop the rest.
513062 -rwxrwxrwx+ 1 smith dev 10876 May 16 9:42 part2
Your first column is the i-node for this file, the column indicates that you have a file where the user, group and other permissions are rwx respectively. The next indicates column the # of files in that directory. the next 2 columns indicate the user and the group that own this file. The next is the total number of bytes in the file, then the timestamp when the file was last modified, followed by the file name.
Lets say you redirect your ls -al contents to a file (ls -al > contents). You can then open your contents file and read in the values into a list of some sort. Each object in the list may have the following elements. The only values you will really need to parse out are the permissions and perhaps the timestamp. The rest of it is pretty standard and you should just be able to read it in.
C++ Syntax (Toggle Plain Text)
class myLsContents{ private: unsigned int inode; string permissions; bool isDirectory; int dirFiles; string username; string groupname; string filesize; // object to store the timestamp; string filename; }
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
'Skatamatic', what u mean is, write a loop to read each character and then when it bum into a space, it means a new column? I can't really do that right, cos looking at the time stamp, there is a space between nov2 19:51
'stillearning' , do u mean the same things when u said reading in the values into the list?
'stillearning' , do u mean the same things when u said reading in the values into the list?
![]() |
Other Threads in the C++ Forum
- Previous Thread: code compiles but does not produce answer!!
- Next Thread: Random array indexes
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






