简体   繁体   English

C ++文件输入在线读取3个整数

[英]C++ File Input 3 ints on line reading

so I have a file for C++ that I am reading in. It has 3 ints 4 4 16 looks like that. 所以我有一个要读取的C ++文件。它具有3个整数4 4 16看起来像这样。 I'm curious as to how I go about getting this all read into 3 variables that I have cars carCapacity people 我很好奇如何将所有这些信息读入我拥有cars carCapacity people 3个变量中

I have set up two different ways of trying to get them but neither are working. 我已经建立了两种尝试获取它们的方法,但是都没有用。 I just get an infinite loop 我只是陷入无限循环

fin >> cars >> carCapacity >> people; Is one way that I'm trying to store the input. 这是我尝试存储输入的一种方法。 another was: 另一个是:

fin >> cars;

fin >> carCapacity

fin >> people

I'm using an ifstream to get the file, I check to make sure that there is a file and then I use while(!fin.eof()) to loop the entire txt file. 我正在使用ifstream来获取文件,我检查以确保有文件,然后使用while(!fin.eof())循环整个txt文件。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!

The easiest way is to use "freeopen" 最简单的方法是使用“ freeopen”

file: a.txt
1 2 3

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
   freeopen("a.txt", "r", stdin);
   int a, b, c;
   std::cin >> a >> b >> c;
   std::cout << a << b << c;
}

P/s: I don't test P / s:我不测试

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

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