0

这是我第一次向 Stack Overflow 发布问题。我是编程新手,所以如果我说一些奇怪或错误的话,请原谅。在下面的文件中;它读取目录并将其保存到变量 nAddress。然后删除文件扩展名;将文件分成 700 行,每行重新构建扩展;最后,将文件名增加 1 个字母 IE:testA、testB、testC、testD 等。

改写: 电流输出:

测试是 1400 行,所以它输出

测试A

测试B

它需要:

测试1

测试2

你能指出我正确的方向吗?谢谢!

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...\n\n";
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....\n\n";
//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] = '\0';
  //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];
}
4

3 回答 3

3

使用某物 像这样:

std::string getPartFilename(int partNumber)
{
    std::ostringstream oss;

    oss << "Test" << partNumber;
    return oss.str();
}

更新 澄清我的观点:重构代码以删除所有用于构建文件名的讨厌的 c 字符串操作(strcpy_s()、、strcat_s()等),并使用简单直接的 C++ 标准机制根据需要格式化字符串。

于 2013-06-20T20:42:55.410 回答
2

好的,所以如果

nAddress[n] = i + 65; 

确实是文件的递增字母被设置的地方,而不是我要做的。

因为您使用的是 std:string,

// make your address just "test"
nAddress[n] = '\0';

// 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] = '\0';  

// 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); 

正如个人提到的


于 2013-06-20T20:42:32.017 回答
0

要将字母更改为数字(如果我理解代码正确),

nAddress[n] = i + 65;

应该成为

nAddress[n] = i + '0';

于 2013-06-20T20:46:12.357 回答