Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:54:21+00:00 2026-06-16T03:54:21+00:00

Basic Question: If I’m storying/modifying data, should I access elements of a file by

  • 0

Basic Question:

If I’m storying/modifying data, should I access elements of a file by index hard-coded index, i.e. targetFile.getElement(5); via a hardcoded identifier (internally translated into index), i.e. target.getElementWithID("Desired Element"), or with some intermediate DESIRED_ELEMENT = 5; ... target.getElement(DESIRED_ELEMENT), etc.

Edit: Or something using an enum ??


Background:

My program (c++) stores data in lots of different ‘dataFile’s. I also keep a list of all of the data-files in another file—a ‘listFile’—which also stores some of each one’s properties (see below, but i.e. what it’s name is, how many lines of information it has etc.). There is an object which manages the data files and the list file, call it a ‘fileKeeper’.

The entries of a listFile look something like:

filename , contents name , number of lines , some more numbers ...

Its definitely possible that I may add / remove fields from this list — but in general, they’ll stay static.

Right now, I have a constant string array which holds the identification of each element in each entry, something like:

const string fileKeeper::idKeys[] = { "FileName" , "Contents" , "NumLines" ... };
const int fileKeeper::idKeysNum = 6;   // 6 - for example

I’m trying to manage this stuff in ‘good’ programatic form. Thus, when I want to retrieve the number of lines in a file (for example), instead of having a method which just retrieves the ‘3’rd element… Instead I do something like:

string desiredID = "NumLines";
int desiredIndex = indexForID(desiredID);
string desiredElement = elementForIndex(desiredIndex);

where the function indexForID() goes through the entries of idKeys until it finds desiredID then returns the index it corresponds to. And elementForIndex(index) actually goes into the listFile to retrieve the index’th element of the comma-delimited string.


Problem:

This still seems pretty ugly / poor-form. Is there a way I should be doing this? If not, what are some general ways in which this is usually done?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:54:22+00:00Added an answer on June 16, 2026 at 3:54 am

    My solution is using a combination of a std::map, and an enum:

    class FileKeeper {
        public:
            enum ID_KEY = { FileName , Contents , "Num_Lines };
            string getIDKey(ID_KEY idk);
    
        private:
            static map<int,string> ID_Key = { { FileName, "Filename" }, { Contents, "Contents" }, { Num_Lines, "Num Lines" } };
    
    }
    
    string FileKeeper::getIDKey(ID_KEY idk) {
        if( idk > 0 && idk < ID_Key.size() ) { return ID_Key[idk]; }
        else { return ""; }
    }
    

    This way I can easily access the indices and iterate through the keys easily (in the proper order — which a map<string,int> wouldn’t do) using the enum, and still retrieve the mapping from index to string using the map. Using a get() method prevents adding elements to the map, and keeping the actual map private keeps me from accessing it lazily using integers (even though it is technically of int-index type).

    No where in the code do I need to access elements using the actual string idenitified, i.e. get___(“Num Lines”); — which is kind of ugly, and seems to be a faux pas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic question here. I started using ARC and am not sure if I should
Basic question - is it possible to access the current Page from a static
Basic question. char new_str[]=; char * newstr; If I have to concatenate some data
Basic question. Instead of adding '\n' between the elements: >> puts #{[1, 2, 3].join('\n')}
Basic question: what design principles should one follow when choosing between using a class
BASIC QUESTION: Is there a way to set the mime-type (content-type) of elements inside
Basic question here - I have many lines of code that look something like:
Basic question.. had to ask. Any help will be appreciated. Q: Why can't I
Basic question so I feel dumb but..., Whats the proper syntax below in the
Basic question! I have 2 tables PRODUCE +-----+--------------+ | id | fruit_name | +--------------------+

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.