Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ stdio.h rename function problem
#1
I'm using the header file standard for C++ stdio.h, specifically for the rename function, however one problem to me that I see is that it expects input parameters for const char strings.

The big problem here is the const part that i'm trying to deal with to coincide with what it wants as my inputs. If it wasn't for that everything would be fine...

Here's what i've got so far (Ignore the Usage.h and Usage function, it's not the problem here and that works fine, it's more the loop right around where i'm trying to rename to the new value)

Code:
#include "stdafx.h"
#include "stdio.h"
#include "Usage.h"

#include <string>
#include <iostream>

int main(int argc, char *arg[])
{
    system("color 0F");

    if(argc < 2)
        Usage();
    else
        std::cout << "Beginning Process - Please Wait...\n\n";

    const char* inputf;
    short unsigned int inputl = 0;

    for (__int64 x = 1; x < argc; x++)
    {
        inputf = arg[x];
        inputl = strlen(inputf);
        char Arr[500];

        for (short unsigned int i = 0; i < inputl; i++)
        {
            if (i == 0 || Arr[i-1] == '\\' || Arr[i-1] == ' ') {
                Arr[i] = char(toupper( inputf[i]) );
            } else {
                Arr[i] = char(tolower( inputf[i] ));
            }
        }

        short unsigned int ren_task = 0;
        ren_task = rename(inputf, Arr);

        if ( ren_task == 0 )
            std::cout << "File successfully renamed:\n\t--" << arg[x] << "\n\n";
        else
            std::cout << "Error renaming file: \n\t--" << arg[x] << "\n\n";

    }
    return 0;
    std::cin.get();
}

This line here:
Code:
ren_task = rename(inputf, Arr);

Arr won't convert to a proper "const char *" input, I get a weird value when trying to parse that out. I have tried seeing if I can cast a string from the std namespace over to that specifier, but no avail. It asks for a const value and therefore doesn't want to accept psuedo-const values calculated upon runtime (which is what my loop does), it wants a definite value that it can take in before calculating it to ensure that it stays it's const value and it's a valid value before startup.

Maybe it's just because it's now 3:45AM in Mountain timezone lol... I'll see tomorrow possibly what's going on.
Reply
#2
Wow, woke up and re-read this... For one, the way i've done it above doesn't even specify Arr as a pointer... What a dumb mistake that was. I still need to find a way to define a full const of pointer type char though, to be used.

Problem I can't wrap my head around is the fact that dereferencing the char pointer will automatically call it to not accept a char string, and while it's dereferenced I can't change it's value because it's like trying to change the const char * variable directly, which is prohibited because of it's const property. While if I try to call it's address, I can't add onto the variable because i'm dealing with memory addresses, and not it's base value, therefore I can't add anything to it to make it a char string because I would be adding onto the memory address which would be bad.

Edit: A bit farther but this is why I like using std:Confusedtring and I hate using character strings... I don't know why they can't just accept input from std:Confusedtring...
Reply
#3
Finished something that works, but still having issues with selecting more than one file. It seems It doesn't want to rename more than 4 files for some reason.

Code:
[Code Removed]

Edit: Wow... It does actually work, for some reason the explorer view just doesn't get refreshed though so you don't see the updated renamed files. Perfect!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Visual stdio [c] RANA 0 578 11-28-2010, 09:21 AM
Last Post: RANA
  [C] Factorial function Commodore 1 900 11-28-2010, 07:13 AM
Last Post: RANA
  [C] Prime number function Commodore 2 5,060 10-11-2010, 01:11 PM
Last Post: Commodore
  Problem with a member function of a class charnet3d 2 996 10-31-2009, 03:22 AM
Last Post: charnet3d

Forum Jump:


Users browsing this thread: 1 Guest(s)