2011-03-04 66 views
3

我想要做的是读取文本文件中的一行包含其长度< = 20和两个整数,例如,它可以是这个样子两个字串什么:阅读字符串和整数

Name Surname 1 14 

我知道,如果我读取字符串,字符串将是所有字符,直到空白,但getline()将整行读取为字符串。那么,我该如何阅读这样的一行呢?有没有简单的方法,或者我将不得不使用正则表达式?

+0

你想读这样的东西吗?:(string,string,int,int)= getline(“texfile”)?其中(,,,,)是一个元组 – fpointbin 2011-03-04 21:41:30

+0

您将需要为输入创建某种解析器。 – RageD 2011-03-04 21:42:29

+0

我添加了一个快速示例来帮助您开始回复。 – RageD 2011-03-04 23:00:14

回答

5

也许......

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 
    ifstream file("/home/facu/text.txt", ios::in); 
    string part1, part2; 
    int num1, num2; 

    if(!file) 
     cerr << "Cant open " << endl; 

    while(file >> part1 >> part2 >> num1 >> num2) 
    { 
     cout << part1 << " " << part2 << " " << num1 
     << " " << num2 << endl; 
    } 

    file.close(); 
    return 0; 
} 

当文本的例子。 txt:

JONES JONES 12 14 
MICHAEL MICHAEL 12 100 
DOE DOE 15 20 
2

std::getline()可能是最好的功能。然后创建某种形式的分析器,用空格分隔每个字段。从那里,您可以使用std::stringstream将字段(如果从0开始,则为2和3)转换为整数值。显然,您会想要为任何类型的I/O操作执行某种错误处理。

编辑:这里是我的帮助你开始

// Example 

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

#define FIELD_SIZE 4 // How many fields per line 

// This is just a custom data structure to hold the data you want 
struct myInfo 
{ 
    myInfo(){} // Default ctor 
    myInfo(std::string name1,std::string name2,int n1,int n2) // Ctor accepting all data 
     : first(name1), last(name2), num1(n1), num2(n2) {} 
    std::string first, last; 
    int num1,num2; 
}; 

// Get the initial size of what your array should be 
// This is based on how many '\n's are found 
int getNumLines(const std::string& input) 
{ 
    int retval = 0; 
    for(unsigned int i=0;i<input.length();++i) 
     if(input[i] == '\n') 
      retval++; 
    return retval; 
} 

// Convert string to int 
int convertStringToInt(const std::string& input) 
{ 
    std::stringstream ss(input); 
    int retval; 
    ss >> retval; 
    return retval; 
} 

// Parse input and store it in our structure 
// Not really efficient if you have large datasets to work with 
void parseAndStore(std::string input, myInfo *& myArray, int size) 
{ 
    size_t pos = 0, pos2 = 0; // Temporary position holder 
    std::string first, last, tmp1, tmp2; 
    std::stringstream tmp; 
    int num1, num2; 
    // This is not efficient - it's merely an example 
    for(int i=0;i<size;++i) // How many items to insert 
     for(int j=0;j<FIELD_SIZE;++j) // How many fields 
     { 
      if(j < FIELD_SIZE-1) 
      { 
       pos2 = input.find(" ",pos+1); 
       if(pos2 == std::string::npos || pos2 > input.find('\n',pos)) // Don't run over the next line 
       { 
        pos = input.find('\n',pos); // Next line 
        break; // Error - invalid line format 
       } 
       // Relatively hacky, but this is just an example to give you an idea 
       switch(j) 
       { 
       case 1: 
        last = input.substr(pos,pos2-pos); 
       break; 
       case 2: 
        tmp1 = input.substr(pos,pos2-pos); 
       break; 
       default: 
        first = input.substr(pos,pos2-pos); 
       break; 
       } 
       pos = pos2+1; 
      } else { // All data collected - parse our ints and store it in our structure 
       pos2 = input.find('\n',pos); // End of line 
       tmp2 = input.substr(pos,pos2); 

       // Convert 
       num1 = convertStringToInt(tmp1); 
       num2 = convertStringToInt(tmp2); 

       // Insert it into our array 
       myArray[i] = myInfo(first,last,num1,num2); 

       pos = pos2+1; // Advance position 
      } 
     } 
} 

int main() 
{ 
    // Read your input file - this is just an example 
    std::string input("Joe Serma 1 30\nGeorge Hola 2 17\n"); 

    int size = getNumLines(input); // Parse the number of lines in your input 
    myInfo * myArray = new myInfo[size]; // Allocate a dynamic array to store your data 
    parseAndStore(input,myArray,size); // Parse this string - leave the original in tact though 

    // Print contents of the array 
    for(int i=0;i<size;++i) 
    { 
     std::cout<< std::endl << "=== Person "<< i+1 << "===" << std::endl 
     << "First: "<< myArray[i].first << std::endl 
     << "Last: "<< myArray[i].last << std::endl 
     << "Num1: "<< myArray[i].num1 << std::endl 
     << "Num2: "<< myArray[i].num2 << std::endl 
     << "Num1+Num2 (For confidence of true int): "<< myArray[i].num1+myArray[i].num2 << std::endl; 
    } 

    delete [] myArray; // Cleanup 

    return 0; 
} 

问候,
丹尼斯M.