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 882107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:22:31+00:00 2026-05-15T12:22:31+00:00

I have a file containing lines of the form, double mass, string seq, int

  • 0

I have a file containing lines of the form,

double mass, string seq, int K, int TS, int M, [variable number of ints]
688.83       AFTDSK      1      1       0       3384 2399 1200
790.00       MDSSTK      1      3       1       342 2

I need a (preferably simple) way of parsing this file without boost. If the number of values per line had been constant then I would have used the solution here.

Each line will become an object of class Peptide:

class Peptide {
    public:
        double mass;
        string sequence;
        int numK;
        int numPTS;
        int numM;
        set<int> parents;
 }

The first three integers have specific variable names in the object while all the following integers need to be inserted into a set.


I was fortunate enough to get two really awesome responses but the run time differences made the C implementation the best answer for me.

  • 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-05-15T12:22:32+00:00Added an answer on May 15, 2026 at 12:22 pm

    The best way I know of to parse an ascii text file is to read it line-by-line and use strtok. It’s a C function, but it’ll break your input into individual tokens for you. Then, you can use the string parsing functions atoi and strtod to parse your numeric values. For the file format you specified, I’d do something like this:

      string line;
      ifstream f(argv[1]);
      if(!f.is_open()) {
        cout << "The file you specified could not be read." << endl;
        return 1;
      }
    
      while(!f.eof()) {
        getline(f, line);
        if(line == "" || line[0] == '#') continue;
    
        char *ptr, *buf;
        buf = new char[line.size() + 1];
        strcpy(buf, line.c_str());
    
        Peptide pep;
        pep.mass     = strtod(strtok(buf, " "), NULL);
        pep.sequence = strtok(NULL, " ");
        pep.numK     = strtol(strtok(NULL, " "), NULL, 10);
        pep.numPTS   = strtol(strtok(NULL, " "), NULL, 10);
        pep.numM     = strtol(strtok(NULL, " "), NULL, 10);
        while(ptr = strtok(NULL, " "))
          pep.parents.insert(strtol(ptr, NULL, 10));
    
        cout << "mass: " << mass << endl
             << "sequence: " << sequence << endl
             << "numK: " << numK << endl
             << "numPTS: " << numPTS << endl
             << "numM: " << numM << endl
             << "parents:" << endl;
    
        set<int>::iterator it;
        for(it = parents.begin(); it != parents.end(); it++)
          cout << "\t- " << *it << endl;
      }
      f.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a binary file (generated with Java) containing a 32 bit int
I have a plain text file looking like this: some text containing line breaks
I have a .xib file containing a UIView and 2 UILabel subviews linked to
I have a c++ header file containing a class. I want to use this
I have a ListView containing file names. These file names need to be draggable
I have a single spool mbox file that was created with evolution, containing a
In the filesystem I have /file.aspx /directory/default.aspx I want to configure IIS so that
I have a file in the following format: Data Data Data [Start] Data I
I have Windows File sharing enabled on an OS X 10.4 computer. It's accessible
I have a file that I want to include in Python but the included

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.