2013-06-20 35 views
0

這是我第一次發佈Stack Overflow的問題。我是編程新手,所以如果我說出奇怪或錯誤的話,請原諒我。在下面的文件中;它讀取目錄並將其保存到變量nAddress。然後它刪除文件擴展名;將文件分解成700行,每個重建擴展;以及最後,遞增1個字母IE的文件名:種皮,TESTB,TESTC,TESTD等C++字母 - >數字

重述: 電流輸出:

試驗是1400行,以便將其輸出

種皮

TESTB

它需要:

測試1

的Test2

你能指出我在正確的方向?謝謝!

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]; 
} 
+0

此行n地址[N-20];什麼也沒做。這是一個錯字嗎?另外,請在啓用警告的情況下進行編譯並修復它們以幫助其他代碼更加快樂。 –

+0

你說得對。當我把事情弄糟的時候,我一直沒有清理過。有幾條線評論說我一直在玩。 – user2506739

+0

@ user2506739有什麼幫助?你有一個可以接受的答案嗎? –

回答

2

好,所以如果

nAddress[n] = i + 65; 

是真正該文件的增量信被設定,不是這裏就是我想要做的。

,因爲你正在使用STD:字符串,

// 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:字符串你接近它這樣

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

爲逐張提到


+0

當然,這個解決方案是他希望在下週教育部門要求他處理更多種類的輸入時要求的解決方案... –

+0

我錯過了關於問題的[tag:c]標記嗎? –

+0

再次,不是一項家庭作業。 – user2506739

3

使用某事物像這樣:

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

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

UPDATE 爲了澄清我的觀點:重構代碼移除所有那些討厭的C字符串操作(strcpy_s()strcat_s()等)的建立文件名,並使用一個簡單的C++標準機制來根據需要格式化字符串。

0

要改變信號碼(如果我理解代碼正確的),

​​3210

應該成爲

nAddress[n] = i + '0';

+0

這樣做。哇。我一直在嘗試一切。謝謝! – user2506739

+2

「我」的值有多遠?爲'i'插入11,會顯示什麼?嗯,甚至沒有更深入的瞭解。也許這種笨拙的代碼是這樣工作的...... –

+0

一致認爲它笨拙,並不會超過9,但他的代碼無法用於更高的數字,所以我給了他最簡單的路徑。我的猜測是,nAddress [n]已經限制了打印的高度,所以這個作業分配將起作用。 –