[英]How to skip integers in C++ taken from a fstream txt file?
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. 并且为SKIP_NUMBER
变量分配了3,该函数将获取的数字为82。
The function must continue to grab the integers every SKIP_NUMBER
until it reaches the end of the file. 该函数必须继续每个SKIP_NUMBER
抓取整数,直到到达文件末尾。 These integers taken from the txt file are then placed into another text file. 然后将从txt文件中获取的这些整数放入另一个文本文件中。
Please help I'm really lost on how to create this loop! 请帮助我在如何创建此循环方面真的迷路了! :D :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;
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!
This smells strongly of homework, especially since a quick check showed similar postings on other forums. 这强烈地散发出家庭作业的气味,尤其是因为快速检查显示在其他论坛上有类似的帖子。
I'm not giving you the answer, but a basic thought process would be something along the lines of... 我没有给您答案,但是基本的思考过程将是...
There will be other possible ways of going about this, but this should be somewhat solid. 还有其他可能的方法可以解决此问题,但这应该是可靠的。 You'll have to do the work yourself and I wasn't very specific about most of the pitfalls. 您必须自己完成这项工作,而我对大多数陷阱并不太明确。 Be thorough. 要彻底
My suggestion of how I would approach this: 我对如何处理此问题的建议:
There are many ways of solving the problem of course. 解决问题的方法有很多种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.