Using the "system" fxn & Operations on its output

I am trying to use the system function to create a string that is my present working directory.

After getting the directory string, I would like to take off the last directory in the string.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 1 #include <iostream>
 2 #include <string>
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9   string str, str2;
10   str = system("pwd");
11   size_t end = str.length()-3;
12   str2 = str.substr(0,end);
13   cout << str2 << endl;
14   return 0;
15 }      


But my output to the screen is the actual pwd with an extra space beneath it.
Also I am not completely sure if I am including the right header files.

So how can I assign my pwd to a string and then do operations on that string?

Cheers,
Justin
Does system return a string on your mahcine? On most, it does not. system does not return whatever the program you call puts on screen. Don't mix up what the system call puts to screen with what your cout puts to screen.

http://en.cppreference.com/w/cpp/utility/program/system

Asking the operating system what the pwd is differs from OS to OS.

Linux has get_current_dir_name and chums ( http://linux.die.net/man/3/getcwd )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

  using namespace std;
  
  int main()
  {
   string str, str2;
   str = system("pwd");
   cout << "Str actually is: " << str << endl;
   char* cwd  = get_current_dir_name();
    cout << "pwd actually is: " << cwd << endl;
   
   return 0;
 }    
Last edited on
Good point, I don't believe the output is a string. I'm on Mac OS-X 10.7 and I found that getcwd works.

This code compiles fine and gives me the desired output. Hopefully it's not too messy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
  
int main()
{
   char *path = NULL;
   size_t size;
   path = getcwd(path,size);
   
   string str, str2;
   stringstream complete;
   complete << path;
   str = complete.str();
   size_t end = str.length()-3;
   str2 = str.substr(0,end);
   cout << str << endl;
   return 0; 
}


Thanks,
Justin
path = getcwd(path,size);


size is some random value. At the moment you're lucky(?) in that it's big enough that when getcwd allocates some memory, size is big enough to hold the whole pathname, but you really shouldn't leave that to chance. Give size a value.



Another good point! This is a finalized version, which I made into a fxn that gives me the desired output.

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
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
using namespace std;

string get_base_directory()
{
/// This fxn finds the path before "preps"
/// No input
/// Only one output, the formatted path as a string
        char *path = NULL;
	size_t size, pos;
	size = MAXPATHLEN;
	path = getcwd(path,size);
	// Convert to string
	string str, str2;
	stringstream complete;
	complete << path;
	str = complete.str();
	// Find "preps"
	pos = str.find("preps");
	// Format path
	str2 = str.substr(0,pos);
	return str2;
}
Topic archived. No new replies allowed.