How to skip integers in C ++ taken from tstream fstream file?

I need to create a function that uses a loop. This function will open a text file and then be able to skip a variable number of leading random numbers. The program should be able to handle any number of leading random integers.

Example, if an open file reads this in its first line:

100 120 92 82 38 49 102

and the variable is SKIP_NUMBERassigned 3 number, which will be captured by function 82.

The function should continue to capture integers every SKIP_NUMBERuntil it reaches the end of the file. These integers taken from the txt file are then placed in another text file.

Please help, I really lost how to create this loop !: D

Here is my function so far ...

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

return 0; //temporary return
}

These are my program variables:

// initialize function/variables

ifstream fin;
string IN_FILE_NAME, OUT_FILE_NAME;
int SKIP_NUMBER;
-1
3

, :

1. create ifstream object m_strm
2. open the file
3. whie (m_strm.good())
    (a.) use ifstream getline() to read a line from the file
    (b.) use strtok() function to tokenize the string (for whitespaces)
    (c.) maintain a counter when you keep getting tokens
    (d.) Now you can skip whenever you like.
4. Done with file, so close the stream!
+2

, .

, - ...

  • :
  • if (counter > SKIP_NUMBER) - reset.

, . , . .

0

, :

  • Write a function to read all numbers from a file and store them in an array / vector / list / independently.
  • Write a function that traverses the data structure, selects each (N + 1) th element and writes them to a file.

Of course, there are many ways to solve the problem.

0
source

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


All Articles