C++ 有办法吗?检查打开的 fstream 文件的一行是否包含多个整数?

发布于 2025-01-11 07:08:58 字数 342 浏览 3 评论 0原文

我用以下文本打开了 myfile.in:Going

1
4 5
3

for every number while (myfile >> var) cout <<变量 << endl; 会一一输出每个整数:

1
4
5
3

有什么办法可以检查 myfile.in 的所有行并输出超过 1 个整数的行吗?对于上面的例子:

4 5

或者

4
5

I have myfile.in opened with the following text:

1
4 5
3

Going for every number while (myfile >> var) cout << var << endl; will output each integer one by one:

1
4
5
3

Is there any way I can check all the lines of myfile.in and output the lines with more than 1 integer? For the example above:

4 5

or

4
5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

櫻之舞 2025-01-18 07:08:58

您可以结合使用 std::getlinestd::istringstream,如下所示。注释中给出了解释。

#include <iostream>
#include<fstream>
#include<string>
#include <sstream>

int main()
{
    std::ifstream inputFile("input.txt");
    std::string line; 
    int num = 0; //for storing the current number
    int count = 0;//for checking if the current line contains more than 1 integer
    if(inputFile)
    {
        //go line by line 
        while(std::getline(inputFile, line))
        {
            std::istringstream ss(line);
            //go int by int 
            while(ss >> num)
            {
                ++count;
                //check if count is greater than 1 meaning we are checking if line contains more than 1 integers and if yes then print out the line and break
                if (count > 1)
                {
                    std::cout<<line <<std::endl;
                    break;
                }
                
            }
            //reset count and num for next line 
            count = 0;
            num = 0;
            
        }
        
    }
    else 
    {
        std::cout<<"input file cannot be opened"<<std::endl;
    }
    return 0;
}

演示

You can use a combination of std::getline and std::istringstream as shown below. The explanation is given in the comments.

#include <iostream>
#include<fstream>
#include<string>
#include <sstream>

int main()
{
    std::ifstream inputFile("input.txt");
    std::string line; 
    int num = 0; //for storing the current number
    int count = 0;//for checking if the current line contains more than 1 integer
    if(inputFile)
    {
        //go line by line 
        while(std::getline(inputFile, line))
        {
            std::istringstream ss(line);
            //go int by int 
            while(ss >> num)
            {
                ++count;
                //check if count is greater than 1 meaning we are checking if line contains more than 1 integers and if yes then print out the line and break
                if (count > 1)
                {
                    std::cout<<line <<std::endl;
                    break;
                }
                
            }
            //reset count and num for next line 
            count = 0;
            num = 0;
            
        }
        
    }
    else 
    {
        std::cout<<"input file cannot be opened"<<std::endl;
    }
    return 0;
}

Demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文