Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Reading tab-delimited input and skipping blank fields

I am making a program that will take a long string of tab-delimited metadata pasted into the console by the user and split them into their correct variables. I have completed the code to split the line up by tab, but there are empty fields that should be skipped in order to put the correct metadata into the correct string variable, which I can't get to work.

Here is the code that 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 inputing 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 like altTitle and description should be blank and skipped. Any help would be much appreciated.

like image 867
penguin Avatar asked Dec 14 '25 12:12

penguin


1 Answers

You've solved the issue with spaces in the fields in an elegant manner. Unfortunately, operator>> will skip consecutive tabs, as if they were one single separator. So, good bye the empty fields ?

One easy way to do it is to use getline() to read individual string fields:

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

Another way is

like image 132
Christophe Avatar answered Dec 17 '25 02:12

Christophe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!