如何从一个fstream txt文件中跳过C ++中的整数? [英] How to skip integers in C++ taken from a fstream txt file?

查看:360
本文介绍了如何从一个fstream txt文件中跳过C ++中的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个使用循环的函数。此函数将打开一个文本文件,然后必须能够跳过可变数量的前导随机整数。程序必须能够处理任意数量的前导随机整数。

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

并且 SKIP_NUMBER 变量已分配3,函数将抓取的数字为82.

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

函数必须继续获取每个 SKIP_NUMBER 的整数,直到它到达文件的结尾。这些从txt文件中获取的整数然后被放置到另一个文本文件中。

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.

请帮助我真的迷失在如何创建这个循环! :D

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
}

这些是我的程序变量:

// 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!

这篇关于如何从一个fstream txt文件中跳过C ++中的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆