C++字母 - >数字
C++ Letters -> Numbers
这是我第一次在Stack Overflow上发问题。我是编程新手,所以如果我说了一些奇怪或错误的话,请原谅我。在下面的文件中;它读取目录并将其保存到一个变量nAddress中。然后删除文件扩展名;将文件分成700行,每行重建扩展名;最后,文件名增加1个字母,如:testA, testB, testC, testD等。
改:电流输出:
测试是1400行,所以它输出
外种皮
的
必须是:
Test1Test2
你能给我指个正确的方向吗?谢谢!string fAddress = argv[1];
if (argc > 2)
{
for (int i = 2; i < argc; i++)
{
string temp = argv[i];
fAddress = fAddress + " " + temp;
}
}
cout << fAddress << "n" <<endl;
// Convert to a char*
const size_t newsize = 500;
char nstring[newsize];
strcpy_s(nstring, fAddress.c_str());
strcat_s(nstring, "");
// Convert to a wchar_t*
size_t origsize = strlen(fAddress.c_str()) + 1;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, fAddress.c_str(), _TRUNCATE);
wcscat_s(wcstring, L"");
ifstream inFile;
inFile.open (wcstring);
int index = 0;
string parts[100];
string text;
for (int i = 0; i < 100; i++)
{
parts[i] = "";
}
// get info until ; is found in each line and add it to the array of char*
while ( !inFile.eof( ) )
{
getline(inFile, text, (char)1);
if ( !inFile )
{
if (inFile.eof( ) )
break;
else
{
cout << "File error...n";
break;
system("PAUSE");
}
}
parts[index] += text;
index++;
}
inFile.close();
int n = fAddress.length(); // Get the total size of the file name.
string nAddress = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
cout<<"Removing previous file extension...n";
n = n - 4; //Remove the extension from the output file
cout<<"Removed previous file extension successfully...nn";
cout<< "Building file location and name....n";
for (int i = 0; i < n; i++)
{
nAddress[i] = nstring[i]; //nstring hold the name
}
cout<< "Built successfully....nn";
//Now nAddress is equal to the location and name of the file....
nAddress[n] = '0' ;//'A';
cout<<nAddress[n];
// nAddress[n+1] = 1+48;
//system("cls");
cout<< "Building file extension...n"<< endl;
for (int i = n; i < n+4; i++) // n is whatever the length of the string is. Add 4 chars onto the n.
{
nAddress[i+1] = nstring[i];
fileextension = fileextension + nstring[i]; //This saves off the full file extension for later use. :)
//cout <<nAddress; This seems to build the extension of the file... IE .T, .TA, .TAP
}
cout<< "File extension built successfully...n"<< endl;
nAddress[n+5] = ' ';
//cout<< nAddress;
string files[10];
//This is the part that searches through the file and splits it up I believe.
for (int i = 0; i < index-2; i++)
{
files[i] = parts[0] + parts[i+1] + parts[index-1];
//cout<< files[i]; //This line will output the entire file in the CMD window
}
//system("cls");
// The function below is where the names are dished out
nAddress[n-20];
int counter = 0;
int lastnum;
for (int i = 0; i < index-2; i++)
{
//string myval;
//ostringstream convert;
//counter++;
//convert << counter ;
nAddress[n] = i + 65; //this is the line that gives the letters... it comes in with an A as the first file FYI
//nAddress = nAddress + convert.str();
//cout<<convert.str();
//cout<<counter;
//myval = nAddress[n];
//cout<<myval;
cout<<"Outputting sub-files...n" <<endl;
cout<<nAddress<< "n" << endl;
size_t origsize = strlen(nAddress.c_str()) + 1;
size_t convertedChars = 0;
wchar_t wcstrings[newsize];
mbstowcs_s(&convertedChars, wcstrings, origsize, nAddress.c_str(), _TRUNCATE);
wcscat_s(wcstrings, L"");
ofstream outFile (wcstrings);
outFile << files[i];
}
这样的:
std::string getPartFilename(int partNumber)
{
std::ostringstream oss;
oss << "Test" << partNumber;
return oss.str();
}
为了澄清我的观点:重构你的代码,删除所有那些讨厌的c字符串操作(strcpy_s()
, strcat_s()
等)来构建文件名,并使用一个简单直接的c++标准机制来格式化字符串,因为你需要它们。
好的,那么
nAddress[n] = i + 65;
是真正设置文件递增字母的地方,那么我将这样做:
因为你使用std:string,
// make your address just "test"
nAddress[n] = ' ';
// cast `i` to a string and concatinate
nAddress += to_string(i);
http://www.cplusplus.com/reference/string/to_string/
http://www.cplusplus.com/reference/string/string/operator + =/
如果你没有使用std:string,你可以这样处理它
// make your address just "test"
nAddress[n] = ' ';
// make a character array that contains the character representation of `i`
char buffer[50];
sprintf("%d", i);
// concatinate
strcat(nAddress, buffer);
或者,你可以只做
sprintf(&nAddress[n], "%d", i);
as individual mention
要将字母更改为数字(如果我理解正确的话),
nAddress[n] = i + 65;
变成
nAddress[n] = i + '0';
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- EASTL矢量<向量<int>>连续的
- 将数字打印成文字
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据