1

假设当前目录是/home/xxx/test,有一个名为“test.txt”的文本文件,其中包含一个单词“hello”,一个名为“test.cpp”的文件如下,

#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;

int main ()
{
        char cwd[1024];
        getcwd(cwd, 1024);
        cout << cwd << endl;

        string s;
        ifstream i("test.txt");
        if (!i.good())
                cout << "Can't open test.txt" << endl;

        i >> s;
        i.close();

        cout << s << endl;

        return 0;
}

test> g++ test.cpp
test> ./a.out
/home/xxx/test
hello
test> gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/xxx/test/a.out...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/xxx/test/a.out
/home/xxx
Can't open test.txt


Program exited normally.
(gdb) pwd
Working directory /home/xxx/test.
(gdb) shell pwd
/home/xxx

我的问题是为什么 gdb 切换到无法找到产生“test.txt”的主目录?为什么'pwd'和'shell pwd'给出不同的结果?

谢谢。

4

1 回答 1

1

There is a 'cd ~' in my .cshrc, this is the root cause for the difference between 'shell pwd' and 'pwd'.

After remove it, all the problems are resolved.

Thanks.

于 2013-08-08T12:58:11.270 回答