读取以制表符分隔的输入并跳过空白字段



我正在编写一个程序,该程序将接受用户粘贴到控制台的一长串以制表符分隔的元数据,并将它们拆分为正确的变量。我已经完成了按制表符分隔行的代码,但是为了将正确的元数据放入正确的字符串变量中,应该跳过一些空字段,这是我无法工作的。

这是我到目前为止的代码:

string dummy;
string FAImport;
cin.ignore(1000, 'n');
cout << "nPlease copy and paste the information from the finding aid and press Enter: ";
getline(cin, FAImport);
cout << FAImport;
stringstream ss(FAImport);
auto temp = ctype<char>::classic_table();
vector<ctype<char>::mask> bar(temp, temp + ctype<char>::table_size);
bar[' '] ^= ctype_base::space;
ss.imbue(locale(cin.getloc(), new ctype<char>(bar.data())));
ss >> coTitle >> altTitle >> description >> dateSpan >> edition >> publisher >> 
physicalDescription >> scale >> extentField >> medium >> dimensions >> arrangement >> 
degree >> contributing >> names >> topics >> geoPlaceNames >> genre >> occupations >> 
functions >> subject >> langIN >> audience >> condition >> generalNotes >> collection >> 
linkToFindingAid >> source >> SIRSI >> callNumber;
checkFAImport(); //shows the values of each variable
cout << "nnDone";
使用下面的代码,在输入元数据后得到以下输出:
coTitle = William Gates photograph with Emiliano Zapata
altTitle = 1915
description = 1915
datespan = Electronic version
edition = 1 photograph : sepia ; 11 x 13 cm
publisher = L. Tom Perry Special Collections, Harold B. Lee Library, Brigham Young University
physicalDescription = Photographs
scale = William Gates papers
extentField = http://findingaid.lib.byu.edu/viewItem/MSS%20279/Series%2011/Subseries%205/Item%20979/box%20128/folder%2012
medium = William Gates photograph with Emiliano Zapata; MSS 279; William Gates papers; L. Tom Perry Special Collections; 20th Century Western & Mormon Manuscripts; 1130 Harold B. Lee Library; Brigham Young University; Provo, Utah 84602; http://sc.lib.byu.edu/
dimensions = MSS 279 Series 11 Subseries 5 Item 979 box 128 folder 12
arrangement = 
degree = 
contributing = 
names = 
topics = 
geoPlaceNames = 
genre = 
occupations = 
functions = 
subject = 
langIN = 
audience = 
condition = 
generalNotes = 
collection = 
linkToFindingAid =  
source = 
SIRSI = 
callNumber = 

在这个例子中,像altTitle和description这样的字段应该是空白的,并被跳过。

您已经以一种优雅的方式解决了字段中的空格问题。不幸的是,operator>>将跳过连续的制表符,就好像它们是单个分隔符一样。那么,再见空旷的田野了吗?

一种简单的方法是使用getline()读取单个字符串字段:

getline (ss, coTitle, 't'); 
getline (ss, altTitle, 't'); 
getline (ss, description, 't');
...

相关内容

  • 没有找到相关文章

最新更新