简体   繁体   中英

std::rename() file wont work when using a char variable as arguments (string converted to char)

I tried it without adding the "\\"" on the beginning and end of the string tablename4 and tablename5 before converting it to char, and the rename() function still wouldn't work. It does work by just putting in the actual file name as rename() arguments, but thats not what i want, i want to use char variables

Full compiling code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>  
#include <iomanip>
#include <stdio.h>
using namespace std;
using std::string;

int main()
{

    std::string tablename4 = "hello.txt";
    ifstream in4(tablename4);
    ofstream out8("temp2.txt");

    std::string convertthis2;
    convertthis2 = "\"" + tablename4 + "\"";
    char * tochar2 = new char[convertthis2.length()];
    strcpy_s(tochar2, (convertthis2.length() + 1), convertthis2.c_str());

    std::string convertthis3;
    std::string tablename5 = "temp2.txt";
    convertthis3 = "\"" + tablename5 + "\"";
    char * tochar3 = new char[convertthis3.length()];
    strcpy_s(tochar3, (convertthis3.length() + 1), convertthis3.c_str());
    in4.close();
    out8.close();
    rename(tochar3, tochar2);

    //below is testing to see if it converted right, but these print out fine, as "hello.txt" and "temp2.txt"
    //but i tried it with and without the quotation marks, rename() still doesnt work
    cout << tochar2 << "     " << tochar3;

    return 0;
}

simplifiying slightly and correcting the 2 bugs:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
using namespace std;
using std::string;

int main()
{

    std::string tablename4 = "hello.txt";
    ifstream in4(tablename4);
    ofstream out8("temp2.txt");

    std::string convertthis2 = string("\"") + tablename4 + "\"";

    std::string tablename5 = "temp2.txt";
    std::string convertthis3 = string("\"") + tablename5 + "\"";
    in4.close();
    out8.close();
    rename(convertthis3.c_str(), convertthis2.c_str());

    //below is testing to see if it converted right, but these print out fine, as "hello.txt" and "temp2.txt"
    //but i tried it with and without the quotation marks, rename() still doesnt work
    cout << convertthis2 << "     " << convertthis3 << endl;

    return 0;
}

The problem is that you're allocating an array of bytes which is equal to the length of the string objects and then using those arrays as c-style strings. However a c-style string must have a terminating zero, which you do not add - hence undefined behaviour.

Note the use of the .c_str() member variable above. It does exactly what you want - makes a std::string look like a c-style string in a safe way.

Something to take away from this is the idea that if you find yourself writing c-code, it's because you would benefit from reading the stl documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM