这是我第一次访问此网站,感谢您对我的问题提供任何帮助。我在我的第一个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 :(得分: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;
}
答案 1 :(得分:0)
您可能需要在循环中调用skipNumber(
),并为其提供一个额外的参数ifstream,正如他在答案中所表达的那样。
伪代码(这进入你的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