简体   繁体   English

使用char变量作为参数时,std :: rename()文件将不起作用(将字符串转换为char)

[英]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 我尝试了一下,但没有在字符串tablename4tablename5的开头和结尾添加"\\"" ,然后再将其转换为char, rename()函数仍然不起作用,仅通过输入实际文件名即可作为rename()参数,但这不是我想要的,我想使用char变量

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: 略微简化并纠正2个错误:

#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. 问题是您要分配一个字节数组,该数组等于string对象的长度,然后将这些数组用作c样式字符串。 However a c-style string must have a terminating zero, which you do not add - hence undefined behaviour. 但是,c风格的字符串必须有一个结尾的零,您不能添加该结尾-从而导致不确定的行为。

Note the use of the .c_str() member variable above. 注意上面的.c_str()成员变量的使用。 It does exactly what you want - makes a std::string look like a c-style string in a safe way. 它确实满足您的要求-以安全的方式使std :: string看起来像c样式的字符串。

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. 可以避免这种情况的想法是,如果您发现自己编写C代码,那是因为您将从阅读stl文档中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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