0

hi guys its my first time doing a c++ program to manipulate shell input
I need to create a c++ program which reads input from this unix shell command >> ls -ali
and then im supposed to count the number of dir,write files.
I am not allowed to use the STAT() function from the library.

I dont know whats wrong with my code below....but it always give me a 0 value output. I think the way i read the input from the command ls -ali is wrong

#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
using namespace std;

int main(int argc, char** argv)
{
	char input[150];
	int writeFile = 0;
	int countDir = 0;
	mode_t modeCheck;
	
	while (cin >> input)
	{
		if (modeCheck == S_IFDIR)
		countDir++;
		if (modeCheck == S_IWRITE)
		writeFile++;
	} 
	cout << countDir << endl;
	cout << writeFile << endl;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Agni
0

I'm not too sure, but may be this might help. This was done on windows.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string p;
	while (cin >> p != '\0')
		cout << p << endl;
}

> ls | ./myProg.exe

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.