Renaming Files the Hard Way

I am trying to write a program that will rename files to an organizational standard. For example, MP3s will be renamed to "track number" SPACE "track title." My approach to this problem is that I would copy every single byte of information in the file and then put them into the new, correctly named file. I will delete the original and implement a way to rename multiple files later on in development. Anyway, here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin("C:\\Documents\\Programming\\Files\\07_luciano.pavarotti_miserere.mp3");
    ofstream fout("C:\\Documents\\Programming\\Files\\07 Miserere.mp3");

    char ch;
    while(!fin.eof())
    {
        fin.get(ch);
        fout.put(ch);
    }

    fin.close();
    fout.close();

    return 0;
}


The issue here is that I am not copying every byte and I think it's because the eof marking may appear at random times during the MP3 file. So then I tried some little snippet of code I found here on c++.com:

1
2
3
4
5
6
7
8
9
10
11
pbuf = fin.rdbuf();
size = pbuf->pubseekoff(0,ios_base::end);
pbuf->pubseekpos(0,ios::in);

char ch;
while(size != 0)
{
    fin.get(ch);
    fout.put(ch);
    size--;
}


This produces gobbledygook that looks like the ID3 tags and then a repeated character when output to a text file. So my question is how do I rename a file in this fashion? Is there an easier way? I may be way over complicating things lol. I would like to stay away from using

1
2
3
4
char oldname[] = "oldname.txt";
char newname[] = "newname.txt";

rename(oldname, newname);


because then I have to declare the size of the array and filepaths aren't one specific size.
Instead of going through that entire ream of hassles, why don't you just use the rename() function?
I took your suggestion and did some digging around here and eventually ended up with a way to do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    string oldn;
    string newn;
    oldn = "C:\\Documents\\Programming\\Files\\07-luciano_pavarotti--miserere-_with_zucchero.mp3";
    newn = "C:\\Documents\\Programming\\Files\\07 Miserere.mp3";

    char *cstr1, *cstr2;    //declaration of character string eventually pointing to the location where oldn and newn are stored

    cstr1 = new char [oldn.size()+1];    //allocate memory to hold oldn and newn PLUS the null terminated character
    strcpy(cstr1, oldn.c_str());    //copy to cstr1 the contents of oldn. Note oldn.c_str() is the same as oldn only c_str points to a null terminated character array.

    cstr2 = new char [newn.size()+1];
    strcpy(cstr2, newn.c_str());

    cout << cstr1 << endl << endl;    //checking to see what cstr1 and 2 hold
    cout << cstr2 << endl << endl;

    rename(cstr1, cstr2);    //renaming

    delete []cstr1;
    delete []cstr2;


It would still be interesting to see what the bits were in the MP3 but in terms of renaming things, its solved.
What is the point of
1
2
3
4
    string oldn;
    string newn;
    oldn = "C:\\Documents\\Programming\\Files\\07-luciano_pavarotti--miserere-_with_zucchero.mp3";
    newn = "C:\\Documents\\Programming\\Files\\07 Miserere.mp3";

Why not just
1
2
string oldn("foo"),
       newn("bar");


Also,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   char *cstr1, *cstr2;    //declaration of character string eventually pointing to the location where oldn and newn are stored

    cstr1 = new char [oldn.size()+1];    //allocate memory to hold oldn and newn PLUS the null terminated character
    strcpy(cstr1, oldn.c_str());    //copy to cstr1 the contents of oldn. Note oldn.c_str() is the same as oldn only c_str points to a null terminated character array.

    cstr2 = new char [newn.size()+1];
    strcpy(cstr2, newn.c_str());

    cout << cstr1 << endl << endl;    //checking to see what cstr1 and 2 hold
    cout << cstr2 << endl << endl;

    rename(cstr1, cstr2);    //renaming

    delete []cstr1;
    delete []cstr2;

Could just be
1
2
3
4
cout << oldn << "\n\n"
     << newn << "\n" << endl;

rename(oldn.c_str(), newn.c_str());
1
2
3
4
cout << oldn << "\n\n"
     << newn << "\n" << endl;

rename(oldn.c_str(), newn.c_str());


Huh, didn't realize that lol. That is nice and simple. Thanks you!
Topic archived. No new replies allowed.