Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

Example if the opened file reads this on its first line:

100 120 92 82 38 49 102

and the SKIP_NUMBER variable is assigned 3 the number the function would grab is 82.

The function must continue to grab the integers every SKIP_NUMBER until it reaches the end of the file. These integers taken from the txt file are then placed into another text file.

Please help I'm really lost on 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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
174 views
Welcome To Ask or Share your Answers For Others

1 Answer

If I were you, I would approach this problem like this:

1. create ifstream object m_strm
2. open the file
3. whie (m_strm.good())
    (a.) use ifstream's 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!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...