简体   繁体   English

如何跳过从fstream txt文件获取的C ++中的整数?

[英]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... 我没有给您答案,但是基本的思考过程将是...

  1. open file with fin 用fin打开文件
  2. check for fail 检查失败
  3. while not at the end of file: 而不在文件末尾:
  4. read in the variable 读入变量
  5. increment a skip counter 增加跳过计数器
  6. if( counter > SKIP_NUMBER) - write out the read in value and reset counter. if(counter> SKIP_NUMBER)-写出读入的值并重置计数器。

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: 我对如何处理此问题的建议:

  1. Write a function to read all of the numbers from a file and store them in an array/vector/list/whatever. 编写一个函数以从文件中读取所有数字并将它们存储在数组/向量/列表/任何内容中。
  2. Write a function which traverses the data structure, selects every (N+1)th items, and writes them to a file. 编写一个遍历数据结构,选择第(N + 1)个项目并将其写入文件的函数。

There are many ways of solving the problem of course. 解决问题的方法有很多种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM