2010-03-09 32 views
3

這是我第一次在這個網站上,我感謝任何幫助我的問題。我在我的第一個C++類中,我承認我不是計算機編程的專家,所以如果你可以讓你的答案儘可能地具體化,它將極大地幫助我的初學者地位。創建一個C++程序跳過文本文件中的整數?

我將創建一個解碼加密文本文件的程序。這些文件由幾組十個整數組成。根據文件的不同,在每個字符的編號之前,文件中會有一些隨機值。例如,在一個文件中,可能會有三個隨機數字,而不是表示一個字符的數字,然後是三個隨機數字,而不是代表字符的下一個數字,依此類推。

程序將需要提示用戶輸入文件,然後輸出文件以及在每個合法整數/字符之前跳過的隨機數的數量。請注意,我的程序需要能夠處理任意數量的領先隨機數。這意味着它可能有一個合法的三個隨機數,或之前的三十個隨機數。

這是我迄今爲止的整個計劃。我需要創建的函數叫做skipVariable,如果有人可以幫助我創建這個函數,它會有幫助,我一直在盯着它幾個小時,我無法想象如何完成這個任務。


//Header Files 
#include <iostream> 
#include <fstream> 

using namespace std; 

// Global Constants 

const char SPACE = ' '; 

//Function Prototypes 


/* 
name: programTitle 
input: none 
output: void (string) 
dependencies: none 
process: output string 
*/ 
void programTitle(); 

/* 
name: promptUser 
input: none 
output: void file name and int skip number 
dependencies: none 
process: output string name and skip number int 
*/ 
void promptUser (string &IN_FILE_NAME, string &OUT_FILE_NAME, int &SKIP_NUMBER); 

/* 
name: openInputFile 
input: ifstream &inf, &fileName (string) 
output: good bad file (bool) 
dependencies: none 
process: test if file can be opened/ does it exist 
*/ 
bool openInputFile(ifstream &inf, const string &fileName); 

/* 
name: skipVariable 
input: skip number(integer) 
output: calculated result (int) 
dependencies: none 
process: (skip variable in file and give exstracted number) 
*/ 
int skipVariable (int SKIP_NUMBER); 

// Main function/program 
int main() 
{ 
// initalize function/variables 

ifstream fin; 
string IN_FILE_NAME, OUT_FILE_NAME; 
int SKIP_NUMBER; 

//Print Program Title 
//Function Name: programTitle 
programTitle(); 

//Prompt user for input file name and skip number 
//Function Name: promptUser 
promptUser (IN_FILE_NAME, OUT_FILE_NAME, SKIP_NUMBER); 

//Check it file is usable and openable 
//Function Name: openInputFile 
openInputFile(fin, IN_FILE_NAME); 

//Skip variable number 
//Function Name: skipVariable 
skipVariable (SKIP_NUMBER); 




//Close input file 
fin.close(); 

// make spaces before program end 
cout << endl << endl; 

// End program 
system("pause"); 

return 0; 
} 

// Supporting function implementation 
// 

//Display Program Title 
void programTitle() 
{ 

    // output prompt string 
cout << "  DECODER PROGRAM" << endl; 
cout << "  ==============="; 
cout << endl << endl; 

// void function - no return 
} 


//Prompt user for Input 
void promptUser (string &IN_FILE_NAME, string &OUT_FILE_NAME, int &SKIP_NUMBER) 
{ 
//prompt for an input file name 
cout << "Enter input file name: " ; 
     cin >> IN_FILE_NAME; 
cout << endl; 

//Prompt for an output file name 
cout << "Enter output file name: " ; 
     cin >> OUT_FILE_NAME; 
cout << endl; 

//Prompt for number of items to skip 
cout << "Enter number of items to skip: " ; 
     cin >> SKIP_NUMBER; 
cout << endl << endl << endl; 

// Print process data indication 
cout << "Processing Data . . ." << endl << endl; 

//void function no return 
} 


//Function opens file and checks if file exists 
bool openInputFile(ifstream &inf, const string &fileName) 
{ 
// clear and open input file 
inf.clear(); 
inf.open(fileName.c_str()); 

// return input file condition 
return inf.good(); 

} 


//Functio skips variables and returns needed integer 
int skipVariable (int SKIP_NUMBER) 

//Is the file good/usable 
{ 

//start loop skip valued variable 

     //get number representing character 

     //output as file character 

// return values 
return 0; //temporary return 
} 
+0

請給輸入文件中一行的例子。價值是如何分離的?他們是否分開(否則只有10個字符是可能的,我認爲......)? – foraidt

+0

看到這個帖子的一些細節:http://stackoverflow.com/questions/2406247/how-to-skip-integers-in-c-taken-from-a-fstream-txt-file –

回答

0

你的函數,跳過讀取文件需要一個手柄文件流。

int skipVariable (ifstream const& in, int SKIP_NUMBER) { 
    int r[ 3 ]; /* store the random numbers */ 
    char c; 
    int nchars = 0; /* number of characters read sucessfully from the file */ 
    while (in >> r[ 0 ] >> r[ 1 ] >> r[ 2 ]) { 
     in >> c; /* read a character */ 
     ++nchars; 
     cout << c; /* emit to console for testing */ 
    } 
    nchars; 
} 
0

你可能需要調用skipNumber()在,也爲它提供一個額外的參數,則ifstream的,因爲dirkgently已經顯示出了他的答案。

僞代碼(此進入您的main()函數):

const numbersToSkip = 3; 
string result = ""; 
loop until end of file is reached 
    char c = skipVariabble(ifstream, numbersToSkip); 
    result += c; 
end of loop 

而且skipVariable()看起來是這樣的:

numberCounter = 0; 
while not at end of file 
    read single number 
    numberCounter += 1 
    if(numberVounter modulo 4 equals 0) 
     return number; 
end of loop 

return SPACE; // if we get here, the file end is reached 
相關問題