這是我第一次在這個網站上,我感謝任何幫助我的問題。我在我的第一個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
}
請給輸入文件中一行的例子。價值是如何分離的?他們是否分開(否則只有10個字符是可能的,我認爲......)? – foraidt
看到這個帖子的一些細節:http://stackoverflow.com/questions/2406247/how-to-skip-integers-in-c-taken-from-a-fstream-txt-file –