LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2006, 11:47 AM   #1
rigel_kent
Member
 
Registered: Nov 2004
Posts: 64

Rep: Reputation: 15
Current dir in C


Hi,

Does anyone know how to get the current working directory within a C program?

Kind regards,

Rigel_Kent
 
Old 07-25-2006, 12:03 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Try getcwd(). You will can either do it the most correct way, by allocating a dynamic string, and expanding it if getcwd() truncates. You may be able to use a statically allocated string of length PATH_MAX, but that isn't as guaranteed
 
Old 07-25-2006, 12:04 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
man getcwd
Quote:
NAME
getcwd, get_current_dir_name, getwd - Get current working
directory

SYNOPSIS
#include <unistd.h>

char *getcwd(char *buf, size_t size);
char *get_current_dir_name(void);
char *getwd(char *buf);
...
 
Old 07-25-2006, 02:33 PM   #4
rigel_kent
Member
 
Registered: Nov 2004
Posts: 64

Original Poster
Rep: Reputation: 15
Exclamation

Hi,

Thanks for replying. It's solved...I used:

Code:
  int _MAX_SIZE = 128;  

  char path[_MAX_SIZE]; 

  getcwd (path, _MAX_SIZE);

  fprintf(file_out, "%d\n", path);
Kind regards,

Rigel_Kent

Last edited by rigel_kent; 07-25-2006 at 02:35 PM.
 
Old 07-25-2006, 04:30 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I'm not sure how that can even work, since _MAX_SIZE is not a constant compile-time expression. You are generally supposed to use #define for that in C.

The size you picked is relatively tiny so bad things happening are decently probable.
 
Old 07-25-2006, 05:17 PM   #6
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Quote:
Originally Posted by tuxdev
I'm not sure how that can even work, since _MAX_SIZE is not a constant compile-time expression. You are generally supposed to use #define for that in C.
It's not allowed by the ISO standard, but it does work. This shouldn't be too surprising, as it's pretty trivial for the compiler to convert
Code:
char path[_MAX_SIZE];
into something like
Code:
char *path;
path = malloc(_MAX_SIZE);
GCC is apparently pretty forgiving about such non-standard code. If you want to stick strictly to the standard, compile with the -pedantic option.
 
Old 07-25-2006, 06:04 PM   #7
rigel_kent
Member
 
Registered: Nov 2004
Posts: 64

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tuxdev
I'm not sure how that can even work, since _MAX_SIZE is not a constant compile-time expression. You are generally supposed to use #define for that in C.

The size you picked is relatively tiny so bad things happening are decently probable.
I agree. But if I try to use _MAX_SIZE it gives me an error in the compilation. How should I use it?

Kind regards,

Rigel_Kent
 
Old 07-25-2006, 06:18 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
PATH_MAX is usually the right constant under Linux.

EXAMPLE:
Code:
#include <stdio.h>
#include <limits.h>

int
main ()
{
  printf ("PATH_MAX= %d\n", PATH_MAX);
  return 0;
}

Last edited by paulsm4; 07-25-2006 at 06:22 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command to show only dirs in current dir? Lordandmaker Linux - Newbie 3 01-06-2006 07:11 AM
su - and stay in current dir lord-fu Linux - Newbie 2 11-16-2005 08:08 AM
PWD question : Current dir not full path facets Programming 2 06-09-2004 04:58 AM
Prompt w/ current dir and user Jair Slackware 2 03-15-2004 06:16 PM
How xterm get known that current dir changed? pvv Programming 3 03-15-2004 08:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration