Thread: Advice needed for how to approach project

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    36

    Advice needed for how to approach project

    I am a relatively new programmer with a decent foundation in Java (went through the entire Headfirst book last month), but I am much weaker with C/C++. I plan on going through a more comprehensive training of both at a later date, but at this moment I am committed to doing a project for school in C or C++ in a very short time frame (just over a week). I want to create a command line batch rename utility, i.e.:

    command line input:
    >rename *dog* *cat*

    output:
    dog1.jpg renamed to cat1.jpg
    bigdog.zip renamed to bigcat.zip
    horse.txt not renamed
    etc

    I knew that the first part of my program design would be searching for the replacement string in the existing filenames... but what I am just now coming to realize from scouring the web is that this functionality is not included in standard C/C++ libraries... right? It seems to require interacting with platform API's or using a 3rd party library, none of which I have experience with. At this point in time, I need to choose the right learning path to send myself down for the next few days:

    1) learn the Windows API and interact with it directly from C (or C++?)
    2) learn the "Microsoft Foundation Classes" (will these do what I need?)
    3) learn a 3rd part library (the "Boost" filesystem library was suggested elsewhere)

    If Linux is a better option I'd be receptive to that too.

    Which would be the best option for me, given my experience and short timeframe? Any advice on C vs C++? I understand OO principles well from my Java experience... but I am still new to the C++ syntax so that might just be an added complication.

    Thanks so much in advance for any advice!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll want to pick an OS first. FindFirstFile/FindNextFile is a good start for windows; you'll probably want some sort of regex handler to do the name change -- I don't know of any Windows ones outside of .net, but Boost could be an option. If you're on Linux then readdir + regex.h should get you there.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    36
    Thanks for the reply. I am pretty sure I'd like to stick with Windows.

    I just found out about something called dirent.h, is that something that could help me?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can always be lazy:
    Code:
    system( "dir whatever &> contents.txt" );
    fp = fopen( "contents.txt", "r" );
    if( fp )
        while( fgets( fp, buf, BUFSIZ ) )
        {
            if( strstr( buf, "dog" ) )
                dorename( buf );
        }

    Quzah.
    Last edited by quzah; 07-11-2011 at 08:39 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If the spec is a straight substitution -- arg 1 for arg 2 -- you don't need a regexp, this is a (case insensitive?) substring substitution, which in C means something that starts with strstr() or strcasestr() and continues with some splicing and possibly reallocation.

    Quote Originally Posted by The111 View Post
    this functionality is not included in standard C/C++ libraries... right?
    WRT to filesystem access, no. WRT internal string handling, yes.

    Boost does provide a portable "filesystem" OO lib but this may be of dubious value if all you have to do is make it work on a particular platform.

    Quote Originally Posted by The111 View Post
    I just found out about something called dirent.h, is that something that could help me?
    Sound *nixish to me but that's me. Filesystem stuff (eg, "dir" "ls" read directory) is platform specific.
    Last edited by MK27; 07-11-2011 at 08:45 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    dirent.h is the *nix version.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by The111 View Post
    Thanks for the reply. I am pretty sure I'd like to stick with Windows.

    I just found out about something called dirent.h, is that something that could help me?
    A bit of reading ofr you ... File Management (Windows)

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    36
    Thank you so much everybody for the quick answers.

    Quote Originally Posted by tabstop View Post
    You'll want to pick an OS first. FindFirstFile/FindNextFile is a good start for windows; you'll probably want some sort of regex handler to do the name change -- I don't know of any Windows ones outside of .net, but Boost could be an option. If you're on Linux then readdir + regex.h should get you there.
    Quote Originally Posted by CommonTater View Post
    A bit of reading ofr you ... File Management (Windows)
    Really stupid question incoming:

    I don't really have experience using external API's. So if I am using a function called "FindFirstFile," I like to know where it is defined. So I searched the include folder of my compiler, which contains windows.h and a bunch of similar headers... I searched using wingrep for "FindFirstFile" and only got two results, winbase.h and wininet.h. Neither of these seem to actually define the function. Where is it visibly defined? I always try to understand the big picture, and it's bothering me to call a function I can't see defined. Obviously it is defined somewhere, since when I call it, it works!

    Thanks!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by The111 View Post
    I don't really have experience using external API's. So if I am using a function called "FindFirstFile," I like to know where it is defined. So I searched the include folder of my compiler, which contains windows.h and a bunch of similar headers... I searched using wingrep for "FindFirstFile" and only got two results, winbase.h and wininet.h. Neither of these seem to actually define the function. Where is it visibly defined? I always try to understand the big picture, and it's bothering me to call a function I can't see defined. Obviously it is defined somewhere, since when I call it, it works!

    Thanks!
    The MSDN was good years ago when it came on CDs with MSVC++, and I imagine it's even more complete now. But I don't know if you can actually see the source code to the function you are looking for. It will however show you the headers you need to include to use it, and typically an example of doing so.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by The111 View Post
    Neither of these seem to actually define the function. Where is it visibly defined? I always try to understand the big picture, and it's bothering me to call a function I can't see defined. Obviously it is defined somewhere, since when I call it, it works!

    Thanks!
    Are you looking for the definition or the declaration? I've never actually looked for source code for things like printf, or exp, or any of it (as interesting as it no doubt is). MSDN will have the prototype (i.e. declaration) for the function, and will also tell you what header to include.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by tabstop View Post
    Are you looking for the definition or the declaration? I've never actually looked for source code for things like printf, or exp, or any of it (as interesting as it no doubt is). MSDN will have the prototype (i.e. declaration) for the function, and will also tell you what header to include.
    Well, if you look at the example here:
    FindFirstFile Function (Windows)

    There are only 3 #include lines at the top of the program, and none of those .h files define the function "FindFirstFile" nor is it standard C. Therefore, how can I write a standard C program using a function which isn't defined in any of my included headers? That's where my confusion comes from. :-/ It works, obviously... but not sure how.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you read the page you linked to? It's handy sometimes:
    Quote Originally Posted by MSDN
    Header
    WinBase.h (include Windows.h)
    And there's no need for you to go grubbing through your header files: the prototype is right there at the top:
    Code:
    HANDLE WINAPI FindFirstFile(
      __in   LPCTSTR lpFileName,
      __out  LPWIN32_FIND_DATA lpFindFileData
    );
    You do have to get used to the MSDN style of documentation a bit though. (That is: __in and __out are (I believe) defined away to nothing, but they are there as a hint to you what they are for.)

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just look at the MSDN link quzah gave you (reproduced here for your convenience). It has the function declaration as well as explain the parameters and even provides a short example. The issue is that all these header files include other header files, which include other header files.......

    The best way to navigate the windows API is by using the MSDN API library. Microsoft has a certain way of setting things up which I'm sure makes sense to somebody but isn't worth the hassle in this case. (HINT: It's all part of their plan to take over the world)

    EDIT: Too slow.
    Last edited by AndrewHunter; 07-12-2011 at 07:57 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by The111 View Post
    "FindFirstFile" nor is it standard C. Therefore, how can I write a standard C program using a function which isn't defined in any of my included headers?
    Of course it's not standard. C doesn't care anything about your file structure/system. That's why he said:
    Quote Originally Posted by tabstop View Post
    You'll want to pick an OS first.
    ...
    I don't know of any Windows ones outside of .net, but Boost could be an option. If you're on Linux then readdir + regex.h should get you there.
    You really should pay more attention.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by AndrewHunter View Post
    The issue is that all these header files include other header files, which include other header files.......
    Thank you, I guess that helps most. I did sort of find a reference to it in winbase.h that I missed before. I know I don't *need* this info to solve my problem... it's more just curiosity. Coming from tiny programming tutorials where I can *see* everything I'm #including... it's frustrating to all of a sudden have a bunch of blackbox functions thrust on me... I thought it would be neat to see where they come from, but I guess it was naive to think that source would be right out in the open.

    Quote Originally Posted by quzah View Post
    You really should pay more attention.
    Classy. I guarantee there was a time you were as confused about all this as I was. I am paying attention... I am however still processing.

    Furthermore, your statement "of course" is based on an incorrect assumption (for which I could tell you to pay more attention) that I was intending to declare the unknown or disagree with something currently stated; I was doing neither... it was a rhetorical question intended to explain how I got to my current state of confusion.

    I do however thank you for your help... you are nearly always the first one to reply when I post here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice needed
    By Incantrix in forum Game Programming
    Replies: 4
    Last Post: 10-18-2010, 08:39 PM
  2. c++ project advice
    By ecd in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2003, 12:34 PM
  3. need advice on project
    By lambs4 in forum C Programming
    Replies: 2
    Last Post: 07-23-2003, 01:06 PM
  4. Job advice needed regarding C++
    By joeyzt in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-20-2003, 01:10 AM
  5. advice needed
    By unregisterd in forum C Programming
    Replies: 3
    Last Post: 10-19-2002, 07:04 AM