C ++ Reading tab delimited tabs and skipping blank fields

I am creating a program that will hold a long string of tab-delimited metadata inserted by the user into the console and split it into the correct variables. I've completed the code to split the line up the tab, but there are empty fields that should be skipped to put the correct metadata in the correct string variable, which I can't seem to get it to work.

Here is the code I have so far:

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 << "\n\nDone";

      

With this code, I get this output after entering the metadata:

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 = 

      

In this example, fields such as altTitle and description should be empty and skipped. Any help would be much appreciated.

+3


source to share


1 answer


You have solved the problem with spaces in fields in an elegant way. Unfortunately, operator>>

will skip consecutive tabs as if they were one single separator. So goodbye blank fields?

One easy way to do this is to use getline()

to read individual string fields:



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

      

Another way -

+1


source







All Articles