简体   繁体   English

C ++从文件读取并分配给变量

[英]C++ Read from file and assign to a variables

i have a question. 我有个问题。 I have a file in format like this: 我有一个格式如下的文件:

A B
C D
E F
X Y
X2 Y2
X3 Y3

I know how to assign A,B,C,D,E,F,X,Y (each file i check have this data) but sometimes i have more data like X2 Y2, X3, Y3 and i want to assing these also (without previous knowledge about how many of these are in the file). 我知道如何分配A,B,C,D,E,F,X,Y(我检查的每个文件都有此数据),但有时我有更多数据,例如X2 Y2,X3,Y3,我也想将它们关联起来(无需事先知道文件中有多少个)。

Actually my code looks like this: 实际上我的代码如下所示:

reading >> a >> b >> c >> d >> e >> f >> x >> y;

Could You help me? 你可以帮帮我吗? Thank You 谢谢

This solution with vectors might solve the problem: 使用向量的此解决方案可能会解决以下问题:

#include <vector>
#include <string>
#include <iterator>
#include <iostream>
#include <fstream>

using namespace std;

void getItems(vector<string>& v, string path)
{
    ifstream is(path.c_str());
    istream_iterator<string> start(is), end;
    vector<string> items(start, end);
    v = items;
}

int main()
{
    vector<string> v;
    getItems(v, "C:\test.txt");

    vector<string>::iterator it;
    for (it = v.begin(); it != v.end(); it++)
        cout << *it << endl;

    return 0;
}

Note: here i am assuming that your file is in C:\\test.txt 注意:这里我假设您的文件在C:\\ test.txt中

If I understood you well you want to be sure that all lines in the file must be read. 如果我对您的理解很好,则希望确保必须阅读文件中的所有行。

With the following code I solved the problem for me: 通过以下代码,我为我解决了这个问题:

std::ifstream file_reading( filename_path_.c_str(), std::ios::in );
if( file_reading ) {

  std::string buffer;
  unsigned int line_counter = 0;

  while( std::getline( file_reading, buffer ) ) {

        std::istringstream istr;
        istr.str( buffer );

        if( buffer.empty() ) 
          break;

         istr >> x_array[local_counter] >> y_array[local_counter];

             line_counter++;
       }
}

using getline and a while loop you all get all lines in the file and store them in a std::vector which is resizable. 使用getline和while循环,您都会将文件中的所有行都保存到可调整大小的std :: vector中。 The instruction break will quit if no more data are found in the next line. 如果在下一行找不到更多数据,则指令中断将退出。

You can input all of them as a string. 您可以将它们全部输入为字符串。 Give the header as #input and use gets() function to input the entire input. 将标头指定为#input并使用gets()函数输入整个输入。 Then work on the string. 然后处理字符串。 You can differentiate between the numbers by neglecting space(" ") and new lines ("\\n"). 您可以通过忽略空格(“”)和换行(“ \\ n”)来区分数字。 Hope this helps! 希望这可以帮助!

In the case where you don't know how much data is in the file, it is worth to use a WHILE loop with the condition that you perform the while loop until it reaches the end of the file. 如果您不知道文件中有多少数据,则值得使用WHILE循环,条件是执行while循环直到到达文件末尾。

Something like this (ensure you include: iostream, fstream, and string): 这样的东西(确保您包括:iostream,fstream和string):

int main () {
    string line;
    ifstream thefile ("test.txt");
    if (thefile.is_open()) 
    {
        while (thefile.good() )
        {
            getline (thefile, line);
        }
        thefile.close();
    }
    else 
    {
        cout << "Unable to open\n"; 
    }
    return 0;
}

You can use 2D vector: 您可以使用2D向量:

Input: 输入:

1 2 3
4 5
6
7 8 9

Code: 码:

int  val;
string line;

ifstream myfile ("example.txt");

vector<vector<int> > LIST;

if (myfile.is_open())
{
   while ( getline(myfile,line) )
   {
      vector<int> v;

      istringstream IS(line);

      while( IS >> val)
      {
         v.push_back(val);
      }

      LIST.push_back(v);
   }

  myfile.close();
}

for(int i=0; i<LIST.size(); i++)
{
    for(int j=0; j<LIST[i].size(); j++)
    {
        cout << LIST[i][j] << " ";
    }
    cout << endl;
}

Output: 输出:

1 2 3
4 5
6
7 8 9

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

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