2011-03-04 68 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.