Alright, let's get you straightened out:
- You can't declare
wd
as char[10]
and then try and read 8192
bytes into it -- won't work,
You can't declare wd
and then try and output wd2
-- won't work, and your compiler should be screaming errors at you,
\\n
is a literal "\n"
(two \\
are a literal '\'
) not a newline '\n'
,
#include <limits.h>
and #define _GNU_SOURCE
to make PATH_MAX
available -- which is the maximum length a pathname can be on your system (if any) -- then read that many bytes with getcwd()
.
Putting it altogether, you can do:
#define _GNU_SOURCE /* needed for PATH_MAX */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h> /* needed for PATH_MAX */
int main (void) {
char wd[PATH_MAX]; /* define your buffer with size PATH_MAX */
if (getcwd (wd, PATH_MAX) == NULL) { /* get the same number of bytes */
perror("getcwd");
exit(1);
}
printf ("wd = %s\n", wd); /* output the results using same variable */
}
Compile
With the source in getcwd.c
and ALWAYS compiling with FULL WARNINGS ENABLED, you can do:
$ gcc -Wall -Wextra -pedantic -Wshadow -std=c11 -Ofast -o bin/getcwd getcwd.c
(note: I have a bin
directory in my current directory that I put executables in to keep from cluttering my source directory)
Don't accept code until it compiles without warning. Add -Werror
to treat warnings as errors so you can't cheat.
Example Use/Output
Running the program yields:
$ ./bin/getcwd
wd = /home/david/dev/src-c/tmp/debug
Let me know if you have further questions.