Creating a C ++ program to skip integers in a text file?

This is my first time on this site, and I appreciate any help on my issue. I am in my first class in C ++ and I admit that I am not an expert in computer programming, so if you could make your answers as deep as possible, this would greatly help my newbie condition.

I will create a program that decodes encrypted text files. Files consist of several sets of ten integers. Depending on the file, the file has a number of random values ​​up to a number for each character. For example, a file may have three random numbers than a number representing a character, then another three random numbers than the next number representing a character, etc.

The program should ask the user for an input file, then an output file and the number of random numbers for skips before each legal integer / character. Please note that my program should be able to handle any number of leading random numbers. This means that before the legal one, it can have three random numbers or thirty random numbers.

Here is my entire program so far. The function that I need to create is called skipVariable, if someone could help me create this function, it would be so useful, I looked at it for hours, and I just can’t imagine how to complete this task.


//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
}
+3
source share
2 answers

Your function that skips reading a file will have a file stream handle.

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
source

, 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
0

Source: https://habr.com/ru/post/1736215/


All Articles