gpt4 book ai didi

c++ - 创建一个 C++ 程序来跳过文本文件中的整数?

转载 作者:太空狗 更新时间:2023-10-29 23:20:16 24 4
gpt4 key购买 nike

这是我第一次访问此站点,非常感谢对我的问题的任何帮助。我正在上我的第一堂 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
}

最佳答案

跳过读取文件的函数需要文件流的句柄。

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;
}

关于c++ - 创建一个 C++ 程序来跳过文本文件中的整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2413245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com