在C ++中从文件接收输入 [英] Receving input form a file in C++

查看:107
本文介绍了在C ++中从文件接收输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个文件,其中的数字是以这种形式写的.
2 3
3 4
5 8
1 2

虽然我可以通过这种方法从文件中获取一行,但

#include <   iostream  > 
#include <   fstream  > 
#include <  字符串 > 
使用命名空间std;
int main()
{
  弦线;
  ifstream myfile("example.txt");
  如果(myfile.is_open())
  {
    而(myfile.good())
    {
      getline(myfile,line);
    }
    myfile.close();
  }
  else cout <  <    无法   open   file";  
 
     返回    0;  
 
 }  



但是我正在寻找的是将第一列中的整数存储在数组中,并将第二列中的整数存储在第二个数组中?

解决方案

您可以尝试执行以下操作来解析该行:

#include <vector>

int main ()
{
  string line;
  int iFirstValue;
  int iSecondValue;
  vector<int> vecFirst;
  vector<int> vecSecond;
  
  ifstream myfile ("example.txt");

  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
       getline (myfile,line);

       if (sscanf(line.c_str(), "%d %d", &iFirstValue, &iSecondValue) == 2)
       {
          vecFirst.push_back(iFirstValue);
          vecSecond.push_back(iSecondValue);
       }
    }
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}



除了使用getline()sscanf(),还可以使用流运算符将值直接读取到iFirstValueiSecondValue中.为每行2条,无需读取行,只需读取两个整数并将其推入向量即可.重复直到结束.

 #include   <  向量 > 
 #include   <   fstream  > 
 #include   <   iostream  > 
  int  main()
{
    使用 命名空间 std;
    向量< int> v1,v2;
    ifstream文件(" );
    同时(!!文件)
    {
         int  a,b;
        文件>>一个>> b;
        v1.push_back(a);
        v2.push_back(b);
    }
    cout<< " ;
     for (矢量迭代器i = v1.begin(),end = v1.end(); i!= end; ++ i)
        cout<< * i<< ' ';
    cout ' )'<< endl<< " ;
     for (矢量迭代器i = v2.begin(),end = v2.end(); i!= end; ++ i)
        cout<< * i<< ' ';
    cout ' )'<<恩德尔
    返回  0 ;
} 


在代码中使用向量的另一种方法是:

 结构数据
{
   int  iVal1;
   int  iVal2;
};

向量< data> vcData; 


Hi,
I have a file in which the numbers are written in this form.
2 3
3 4
5 8
1 2

While I am able to get a single line from the file by this method,

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
    }
    myfile.close();
  }
  else cout << "Unable to open file";

  return 0;

}



but what I am looking for is to store the integers from the first column in an array and the integers from the second column in the second array? How can that be done?

解决方案

you can try something like this for parsing the line:

#include <vector>

int main ()
{
  string line;
  int iFirstValue;
  int iSecondValue;
  vector<int> vecFirst;
  vector<int> vecSecond;
  
  ifstream myfile ("example.txt");

  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
       getline (myfile,line);

       if (sscanf(line.c_str(), "%d %d", &iFirstValue, &iSecondValue) == 2)
       {
          vecFirst.push_back(iFirstValue);
          vecSecond.push_back(iSecondValue);
       }
    }
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}



Instead of getline() and sscanf() you can also read values directly into the iFirstValue and iSecondValue using stream operators.


As far numbers are separated by spaces and are always granted to be 2 per line,there is no need to read lines, just read two integers ad push them into the vectors. And repeat until it goes.

#include <vector>
#include <fstream>
#include <iostream>
int main()
{
    using namespace std;
    vector<int> v1, v2;
    ifstream file("example.txt");
    while(!!file)
    {
        int a, b;
        file >> a >> b;
        v1.push_back(a);
        v2.push_back(b);
    }
    cout << "v1 = (";
    for(vector<int> iterator i=v1.begin(), end=v1.end(); i!=end; ++i)
        cout << *i << ' ';
    cout ')' << endl <<"v2 = (";
    for(vector<int> iterator i=v2.begin(), end=v2.end(); i!=end; ++i)
        cout << *i << ' ';
    cout ')' << endl;
    return 0;
}


One more way to use vector in your code is :

struct data
{
  int iVal1;
  int iVal2;
};

vector <data> vcData;


这篇关于在C ++中从文件接收输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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