C++ c++;将整数更改为字符串时,stringstream无法正常工作
我正在尝试做一个功能,将为我创建文件夹,并使用数字组合命名它们。以下是我为函数编写的代码:C++ c++;将整数更改为字符串时,stringstream无法正常工作,c++,string,directory,stringstream,C++,String,Directory,Stringstream,我正在尝试做一个功能,将为我创建文件夹,并使用数字组合命名它们。以下是我为函数编写的代码: void folderMaker(int xCount, int yCount) { stringstream ssObject; string xString; string yString; for (int x = 0; x<xCount; x++) { for (int y = 0; y<yCount; y++) { ssObject
void folderMaker(int xCount, int yCount)
{
stringstream ssObject;
string xString;
string yString;
for (int x = 0; x<xCount; x++)
{
for (int y = 0; y<yCount; y++)
{
ssObject << x;
xString = ssObject.str();
ssObject << y;
yString = ssObject.str();
string nameAndLocation = "C:\\User\\DestinationFolder\\" + xString + "and" + yString;
CreateDirectory (nameAndLocation.c_str(), NULL);
}
}
}
std::string ToString(int number)
{
std::ostringstream os; // new stream created
os << number;
return os.str();
}
void foldermarker(int xCount,int yCount)
{
stringstream对象;
字符串xString;
串珠串;
对于(int x=0;x,因为您一直将数据推送到相同的流中
最简单的方法是为每次转换使用一个新的流,最好是在函数中:
void folderMaker(int xCount, int yCount)
{
stringstream ssObject;
string xString;
string yString;
for (int x = 0; x<xCount; x++)
{
for (int y = 0; y<yCount; y++)
{
ssObject << x;
xString = ssObject.str();
ssObject << y;
yString = ssObject.str();
string nameAndLocation = "C:\\User\\DestinationFolder\\" + xString + "and" + yString;
CreateDirectory (nameAndLocation.c_str(), NULL);
}
}
}
std::string ToString(int number)
{
std::ostringstream os; // new stream created
os << number;
return os.str();
}
std::字符串到字符串(整数)
{
std::ostringstream os;//已创建新流
操作系统,因为您一直将数据推送到相同的流中
最简单的方法是为每次转换使用一个新的流,最好是在函数中:
void folderMaker(int xCount, int yCount)
{
stringstream ssObject;
string xString;
string yString;
for (int x = 0; x<xCount; x++)
{
for (int y = 0; y<yCount; y++)
{
ssObject << x;
xString = ssObject.str();
ssObject << y;
yString = ssObject.str();
string nameAndLocation = "C:\\User\\DestinationFolder\\" + xString + "and" + yString;
CreateDirectory (nameAndLocation.c_str(), NULL);
}
}
}
std::string ToString(int number)
{
std::ostringstream os; // new stream created
os << number;
return os.str();
}
std::字符串到字符串(整数)
{
std::ostringstream os;//已创建新流
os为什么不从头开始在ssObject中构造名称
for (int x = 0; x<xCount; x++)
for (int y = 0; y<yCount; y++) {
stringstream ssObject;
ssObject << "C:\\User\\DestinationFolder\\" << x << "and" << y;
CreateDirectory (ssObject.str().c_str(), NULL);
}
for(int x=0;x为什么不从头开始在ssObject中构造名称
for (int x = 0; x<xCount; x++)
for (int y = 0; y<yCount; y++) {
stringstream ssObject;
ssObject << "C:\\User\\DestinationFolder\\" << x << "and" << y;
CreateDirectory (ssObject.str().c_str(), NULL);
}
for(int x=0;x
第一种形式(1)返回一个字符串对象,其中包含流当前内容的副本
第二种形式(2)将s设置为流的内容,丢弃以前的任何内容
要清理缓冲区,请同时使用第二个重载:
xString = ssObject.str();
ssObject.str("");
// ^^
第一种形式(1)返回一个字符串对象,其中包含流当前内容的副本
第二种形式(2)将s设置为流的内容,丢弃以前的任何内容
要清理缓冲区,请同时使用第二个重载:
xString = ssObject.str();
ssObject.str("");
// ^^
这应该就可以了!!
void folderMaker(int xCount, int yCount)
{
std::stringstream ssObject;
std::string xString;
std::string yString;
for (int x = 0; x<xCount; x++)
{
for (int y = 0; y<yCount; y++)
{
ssObject << "x" << x;
xString = ssObject.str();
ssObject.str("");
ssObject << "y" << y;
yString = ssObject.str();
ssObject.str("");
std::string nameAndLocation = "C:\\User\\DestinationFolder\\"
+ xString
+ "and" + yString;
CreateDirectory (nameAndLocation.c_str(), NULL);
std::cout << nameAndLocation << std::endl;
}
}
}
void foldermarker(int xCount,int yCount)
{
std::stringstream-ssObject;
std::string xString;
std::字符串字符串;
对于(intx=0;x这应该可以解决问题了!!
void folderMaker(int xCount, int yCount)
{
std::stringstream ssObject;
std::string xString;
std::string yString;
for (int x = 0; x<xCount; x++)
{
for (int y = 0; y<yCount; y++)
{
ssObject << "x" << x;
xString = ssObject.str();
ssObject.str("");
ssObject << "y" << y;
yString = ssObject.str();
ssObject.str("");
std::string nameAndLocation = "C:\\User\\DestinationFolder\\"
+ xString
+ "and" + yString;
CreateDirectory (nameAndLocation.c_str(), NULL);
std::cout << nameAndLocation << std::endl;
}
}
}
void foldermarker(int xCount,int yCount)
{
std::stringstream-ssObject;
std::string xString;
std::字符串字符串;
对于(int x=0;xIs设置两次xString和never yString是否正常?ssObject
从未被清除。另外,您的代码没有使用yString
@DOOM我对重用xString很不满意,当我在stackoverflow中重新键入代码时,这是一个错误。我如何清除ssObject DOOM?但为什么要创建3个intermedi用于格式化fodler名称的两个变量,当您只能使用stringstream进行整个格式化时?@Christophe我尝试使用字符串流进行格式化,但是代码不允许我在ssObject中生成字符串,而不首先将x和y整数变量更改为字符串。这就是为什么我必须声明xString和yString.Am我纠正了这样做的方式?你设置两次xString和never-yString是正常的吗?ssObject
从未被清除。而且你的代码没有使用yString
@DOOM我对重用xString很不好,当我在stackoverflow中重新键入代码时,这是一个错误。我如何清除ssObject DOOM?但是为什么创建3个用于格式化fodler名称的中间变量,而您只能使用stringstream进行整个格式化吗?@Christophe我尝试使用字符串流进行格式化,但是代码不允许我在ssObject中创建字符串,除非先将x和y整数变量更改为字符串。这就是我必须声明xSt的原因ring和yString。我这样做正确吗?我明白了。有没有代码可以清除string stream对象?我不能对代码进行太多的更改,因为其他代码依赖于它,如果有一行代码来清除stringstream对象以便可以重用,这对于me@CodeBlocks当前位置见Piotr S.的答案。我明白了。是吗有没有代码可以清除stringstream对象?我不能对代码进行太多的更改,因为其他一些代码依赖于它,如果有一行代码可以清除stringstream对象以便可以重用,这对于me@CodeBlocks:参见Piotr S.的答案。完成任务的是ssObject.str(“”)这正是我所需要的xD!有一天我希望像Piotr S一样出色。完成了这项工作,ssObject.str(“”)正是我所需要的xD!有一天我希望像Piotr S一样出色。是的!我得到了与ssObject.str(“”)相同的解决方案从Piotr S,我希望我能以某种方式把你的两个答案都放在最好的答案上!我得到了与ssObject.str(“”)相同的解决方案从Piotr S.,我希望我能以某种方式把你的两个答案都作为最好的答案。我试过这么做,错误是我不能在你最后的第三行代码中插入x和y变量,因为它们是整数而不是字符串。@CodeBlocks:那是不可能的。你可以将整数传递给流。肯定还有其他原因错。缺少#include?你用+而不是@ChristianHackl了吗?你是对的,我用“+”符号而不是"@Christophe谢谢Christophe,你的方法确实是我尝试过的最好的方法,错误是我无法在你最后的第三行代码中插入x和y变量,因为它们是整数而不是字符串。@CodeBlocks:那是不可能的。你可以将整数传递给流。一定是其他错误。丢失了#include?你是用+而不是@ChristianHackl吗?你是对的,我用“+”符号而不是“@Christophe谢谢Christophe,你的方法确实是最好的