LinuxQuestions.org
Help answer threads with 0 replies.
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 08-13-2008, 07:42 PM   #1
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Rep: Reputation: 15
Unhappy How to get name of files in local directory using C++


Hello,
I am new to Linux and am trying to write a program in C++ for Linux that will concatenate all files in the ./ directory (i.e. local) directory in alphabetical order. I have searched on the web and haven't really found anything that I fully understand. I will greatly appreciate any advice!!!
 
Old 08-13-2008, 08:54 PM   #2
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Found this for you. However, I don't know how to figure out the current directory and my Linux Programming book is at work. I remember seeing it in the book, but I'll look tomorrow.
 
Old 08-13-2008, 09:10 PM   #3
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Wink

Thanks! I will look up the info you provided!
 
Old 08-14-2008, 10:54 AM   #4
CRC123
Member
 
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
Got to my book and found how to get current directory:
Code:
#include <unistd.h>
char *getcwd(char *buf, size_t size);
lol, so easy but so hard to remember

Here's a good overview of how it works.

Good Luck
 
Old 08-14-2008, 02:36 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I believe scandir would be more appropriate. I find it works very well, especially since you can filter and/or sort the entries before the list is complete.
ta0kira

Last edited by ta0kira; 08-14-2008 at 03:12 PM. Reason: accidentally said 'getdirentries'
 
Old 08-14-2008, 03:48 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The usual ways to access directory contents on *nix systems is to use opendir/readdir/close dir or glob. See the manual pages for these commands for more info.

Sorting would likely be a separate activity. You might as well use the STL classes and functions for this.
 
Old 08-14-2008, 05:39 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by matthewg42 View Post
Sorting would likely be a separate activity. You might as well use the STL classes and functions for this.
Or do it all in C with one function call:
Code:
#include <stdio.h>
#include <dirent.h>

int main()
{
    struct dirent **entries = NULL, **current = NULL;

    if (scandir(".", &entries, NULL, &alphasort) != -1)
    {
    if ((current = entries))
    while (*current)
    fprintf(stderr, "%s\n", (*current++)->d_name);
    }

    free(entries);

    return 0;
}
ta0kira

PS If alphasort isn't available on your system, it's pretty simple to create your own:
Code:
#include <string.h>

static int alphasort2(const void *a, const void *b)
{ return strcmp( (*(struct dirent**) a)->d_name, (*(struct dirent**) b)->d_name ); }

Last edited by ta0kira; 08-14-2008 at 05:44 PM.
 
Old 08-14-2008, 10:24 PM   #8
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks!! This is sooo helpful!!
 
  


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
How to allow users to access files in /usr/local/<directory> vitalstrike82 SUSE / openSUSE 3 03-26-2008 10:04 PM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 07:20 AM
Autozipping files from 1 directory & dropping them in other directory ??? amitsharma_26 Linux - Networking 5 10-22-2005 06:09 AM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM

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

All times are GMT -5. The time now is 08:51 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