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

  • Home
  • SEARCH
  • 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 6030315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:01:08+00:00 2026-05-23T05:01:08+00:00

I wrote a program for testing purposes related to my work: #include <iostream> #include

  • 0

I wrote a program for testing purposes related to my work:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <conio.h>

using namespace std;
/* Function to convert an std::string to int*/
int UsingATOI(std::string &str)
{
  std::string strIntString(str);
  int iConvertToMe;
  iConvertToMe = atoi( strIntString.c_str());
  return iConvertToMe;

}

/*function to splt an std::string using a delimiter ,
here a vector is exclusively passed an an argument*/

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) 
{
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) 
    {
        elems.push_back(item);
    }
    return elems;
}

/*function to splt an std::string using a delimiter ,
here a vector is prepared inside the function and returned*/
std::vector<std::string> split(const std::string &s, char delim) 
{
    std::vector<std::string> elems;
    return split(s, delim, elems);
}

int showrecord(vector<std::string> all_lines,int linenumber)
{
      char *a[]={"O_BILLABLE_ACCOUNT","O_CUSTOMER_TYPE_INDICATOR","O_A_PARTY_MSISDN_ID","O_A_PARTY_EQUIPMENT_NUMBER","O_A_PARTY_IMSI","O_A_PARTY_LOCATION_INFO_CELL_ID","O_B_PARTY_MSISDN_ID","O_B_PARTY_EQUIPMENT_NUMBER","O_B_PARTY_IMSI","O_B_PARTY_LOCATION_INFO_CELL_ID","O_APN","O_URL_ADDRESS","O_IP_ADDRESS","O_DIALLED_DIGITS","O_CALL_ESTABLISHMENT_REASON","O_TERMINATION_REASON","O_COMPENSATION_CODE","O_CALL_DIRECTION_CODE","O_EVENT_TYPE","O_EVENT_SUB_TYPE","O_SUPPLEMENTARY_SERVICE","O_CUSTOMER_ACCOUNT_INFORMATION","O_CHARGING_ID","O_PROVIDER_ID","O_ZONE_INDICATOR","O_ROUTING_PREFIX","O_SERVICE_CODE","O_NETWORK","O_ORIGINAL_FILE_NAME","O_MSC_IDENTIFIER","O_CIRCUIT_TRUNK_IDENTIFIERS","O_CALL_SOURCE_TYPE","O_CALL_START_TIMESTAMP","O_CALL_END_TIME","O_UNIT_OF_MEASURE","O_DURATION","O_VOLUME","O_UPLINK_DOWNLINK","O_MESSAGE_SIZE_VIDEO","O_GROSS_CHARGE","O_NET_CHARGE","O_CURRENCY","O_QOS_REQUESTED_RECEIVED","O_INBOUND_ROAMING_INDICATOR","O_OUTBOUND_MSRN","O_CONTENT_TYPE","O_CONTENT_PROVIDER_ID","O_CONTENT_ID","O_CONTENT_DESCRIPTION","O_TON_NPI_CALLING_CALLED","O_IN_GW_GSM_VPN_EVENT_INFO","O_IN_GW_CALL_TYPE","O_IN_GW_TERMINATION_TYPE","O_IN_GW_BUSINESS_GROUP","O_IN_GW_TRANSMISSION_TYPE","O_IN_GW_BUSINESS_PERSONAL_CALL","IN_PRE_INFO","O_CAMEL_SERVICE_KEY","O_CAMEL_SERVICE_LEVEL","O_CAMEL_SERVICE_USED","O_CAMEL_CALL_REFERENCE","O_CAMEL_MSC_ADDRESS","O_CAMEL_DESTINATION_NUMBER","O_ROAMING_TYPE","O_CAMEL_SCP_ADDRESS","O_CALL_REFERENCE","O_CALL_NUMBERS","O_SMS_SERVICECENTREADDRESS","O_VARCHAR_3","O_INPUT_RECORD_TYPE","O_VARCHAR_5","O_VARCHAR_6","O_VARCHAR_7","O_VARCHAR_8","O_VARCHAR_9"};
      vector<std::string> fields(a,a+75);
      vector<std::string> linesplit=split(all_lines[linenumber-1],',');
      vector<std::string>::const_iterator itrecord=linesplit.begin();
      vector<std::string>::const_iterator it=fields.begin();
      int counter=1;
      for(;it!=fields.end()||itrecord!=linesplit.end();counter++,++it,++itrecord)
      {
      cout << counter <<"\t"<<*it<<" "<<*itrecord<<endl;
      }
      return 0;
}

int main(int argc,char **argv)
{

       vector<std::string> lines;
       lines.reserve(5000);
       /*getting the file name*/
       string filename(argv[1]);
       ifstream file;
       file.open(filename.c_str());
       if(!file.is_open())
       {
              cerr<<"Error opening file : "<<filename.c_str()<<endl;
              return -1;
       }
      /*Read the lines and store it in the vector*/
      string line;
      while(getline(file,line))
      {
          lines.push_back(line);
      }
      file.close();
      vector<std::string> lines_in_file=split(argv[2],',');
      string detail_record("-show");

if(!detail_record.compare(*(lines_in_file.begin())))
{
        int linetoshow=UsingATOI(*(++lines_in_file.begin()));
        showrecord(lines,linetoshow);

}
getch();      
}

This is the contents of the input file:

,2,353867835022;11,012268004278220,272023112983396,272-02f-20008-26217,3538353588202;11,,,,,,,08353588202b,3,00,,O,V,VOICE,,,11162812,,,17683,11,HPLMN,M10MSS_TTFILE_0650_20110414204840,M10MSS,W20B13I;0VMSGRI,1,20110414204447,204517,1,30,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,06631f000a,1:353860295822::::08353588202b::3538353588202,,,MOC,,,,1,
,2,353867835022;11,012268004278220,272023112983396,272-02f-20008-26217,3538557137704;11,,,,,,,08557137704b,3,00,,O,V,VOICE,,,11162812,,,17687,11,HPLMN,M10MSS_TTFILE_0650_20110414204840,M10MSS,W20B13I;0VMSGRI,1,20110414204447,204517,1,30,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,06631f000a,1:353860295822::::08557137704b::3538557137704,,,MOC,,,,,
,0,353867835022;11,,272023201187741,,353854100352;11,,,,,,,,3,04,,O,V,IPVMS,SS04,,971062,,,17685,,HPLMN,M30MSS_TTFILE_0511_20110606165900,M30MSS,W30EABI;0VMSGRI,,20110606185900,170900,1,600,,,,,,,,0,,,,,,,,,,,,,,00000014,Basic,,fdc45c001e,353868001830,353868001241,,353868001241,fdc45c001e,,,,,,,,,
,0,353867835022;11,,272023201187741,,353894201740;11,,,,,,,,3,04,,O,V,IPVMS,SS04,,971062,,,17684,,HPLMN,M30MSS_TTFILE_0511_20110606180901,M30MSS,W30EABI;0VMSGRI,,20110606180901,171901,1,600,,,,,,,,0,,,,,,,,,,,,,,00000014,Basic,,fdc45c001e,353868001830,353868001241,,353868001241,fdc45c001e,,,,,,,,,
,0,353867835022;11,,272023201187741,,353833588202;11,,,,,,,,3,04,,O,V,IPVMS,SS04,,971062,,,17683,,HPLMN,M30MSS_TTFILE_0511_20110606181902,M30MSS,W30EABI;0VMSGRI,,20110606181902,172902,1,600,,,,,,,,0,,,,,,,,,,,,,,00000014,Basic,,fdc45c001e,353868001830,353868001241,,353868001241,fdc45c001e,,,,,,,,,
,0,353867835022;11,,272023201187741,,353857137704;11,,,,,,,,3,04,,O,V,IPVMS,SS04,,971062,,,17687,,HPLMN,M30MSS_TTFILE_0511_20110606182903,M30MSS,W30EABI;0VMSGRI,,20110606182903,173903,1,600,,,,,,,,0,,,,,,,,,,,,,,00000014,Basic,,fdc45c001e,353868001830,353868001241,,353868001241,fdc45c001e,,,,,,,,,
,2,353868898655;11,354346049187430,272023112144539,272-02f-20008-24049,353894414476;11,,,,,,,353894414476,3,00,,O,V,VOICE,,,12649734,,,17684,11,HPLMN,M10MSS_TTFILE_6518_20110513104240,M10MSS,W20B13I;0GRI3,1,20110513104023,104130,1,67,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,c6d299000a,1:353868898655::::353894414476::353894414476,,,,,,,,
,2,353872458299;11,358005031789960,272023112578235,272-02f-20025-15107,353876225788;11,,,,,,,353876225788,3,00,,O,V,VOICE,,,12664989,,,17683,11,HPLMN,M10MSS_TTFILE_6518_20110513104240,M10MSS,W20B03I;0GRI3,1,20110513104209,104226,1,17,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,c7266e000a,1:353872458299::::353876225788::353876225788,,,,,,,,
,2,353872889489;11,354866045620160,272023112067271,272-02f-31011-11492,353863475401;11,,,,,,,0863475401,3,00,,O,V,VOICE,,,12668430,,,17685,11,HPLMN,M10MSS_TTFILE_6518_20110513104240,M10MSS,BLRC11I;0GRI3,2,20110513104213,104237,1,24,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,c725a9000a,1:353872889489::::0863475401::353863475401,,,,,,,,
,2,353872889489;11,354866045620160,272023112067271,272-02f-31011-11492,353863475401;11,,,,,,,0863475401,3,00,,O,V,VOICE,,,12668430,,,17687,11,HPLMN,M10MSS_TTFILE_6518_20110513104240,M10MSS,BLRC11I;0GRI3,2,20110513104213,104237,1,24,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001810,,,,c725a9000a,1:353872889489::::0863475401::353863475401,,,,,,,,
,2,353867835022;11,353681041426390,272023201187741,272-02f-20017-06609,353854100351;11,,,,,,,0854100351,3,00,,O,D,DATA,,,7124395,,,17683,16,HPLMN,M20MSS_TTFILE_8377_20110605170245,M20MSS,W30B22I;0GRI3,1,20110605130014,170054,1,41,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001820,,,,b60a5c0014,1:353867835022::::0854100351::353854100351,,,,,,,,
,2,353867835022;11,353681041426390,272023201187741,272-02f-20017-06609,353854100353;11,,,,,,,0854100353,3,00,,O,D,DATA,,,7124395,,,17687,16,HPLMN,M20MSS_TTFILE_8377_20110605170245,M20MSS,W30B22I;0GRI3,1,20110605130014,170054,1,41,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001820,,,,b60a5c0014,1:353867835022::::0854100353::353854100353,,,,,,,,
,2,353867835022;11,353681041426390,272023201187741,272-02f-20017-06609,353894201740;11,,,,,,,0894201740,3,00,,O,D,DATA,,,7124395,,,17684,16,HPLMN,M20MSS_TTFILE_8377_20110605170245,M20MSS,W30B22I;0GRI3,1,20110605140015,170054,1,41,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001820,,,,b60a5c0014,1:353867835022::::0894201740::353894201740,,,,,,,,
,2,353830020353;11,353681041426390,272023201580328,272-02f-20017-06609,353854100352;11,,,,,,,0854100352,3,00,,O,D,DATA,,,7124395,,,17685,16,HPLMN,M20MSS_TTFILE_8377_20110605170245,M20MSS,W30B22I;0GRI3,1,20110605130015,170054,1,41,,,,,,,,0,,,,,,,,,,,,,,,,,,353868001820,,,,b60a5c0014,1:353830020353::::0854100352::353854100352,,,,,,,,

Each line is guaranteed to have 75 fields (meaning 74 commas). There are no compiler errors. I run the program like this:

a.out text_file.txt -show,1

The program crashes with a segmentation fault after printing the last field. What’s wrong with the code? I think the problem is the fact is that even though the line has 75 fields, if the last field is empty, then the next character after the last comma is newline. But in real time I cannot change the input data files but I need to handle this in the code itself if the last field is not present. I did manually add a random number after the 74th comma, then the code works fine without a seg fault.

  • 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-23T05:01:08+00:00Added an answer on May 23, 2026 at 5:01 am

    It’s not at all clear what you’re trying to do, but two things stick
    out:

    • You’re reconstructiong the fields vector every time you enter the function, although you never modify it. I’ld also drop the 75:
        static char const* a[] = { ... };
        static std::vector fields(  begin( a ), end( a ) );
    
    • You’re iterating over the number of fields. What happens if a line
      doesn’t contain that many fields? The end condition should be it !=
      fields.end() && itrecord != linesplit.end()
      . Or you add some earlier
      logic to ensure that there are enough fields.

    • You’re algorithm for split is incorrect. I’ve seen this proposed
      several times, but using getline is not an appropriate solution;
      it fails if it extracts no characters (including no delimiter). You
      could make it work by systematically postfixing a delimiter; i.e.
      std::istringstream ss(s + ' ') as the first line in split, but I
      still find it misleading, and would prefer something more along the
      lines of:

        std::vector
        split( std::string const& source, char delim )
        {
            std::vector results;
            std::string::const_iterator curr = source.begin();
            std::string::const_iterator end = source.end();
            std::string::const_iterator next = std::find( curr, end, delim );
            while ( next != end ) {
                results += std::string( curr, next );
                curr = next + 1;
                next = std::find( curr, end, delim );
            }
            results += std::string( curr, next );
            return results;
        }
    

    One thing which makes me curious: your invocation (a.out ...) is
    clearly Unix, but the code as you post it won’t compile under Unix
    (<conio.h> and getch). So just what platform are you using?

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

Sidebar

Related Questions

I wrote a program using AutoIT to fetch information from a number of websites
I wrote a program to click on an application automatically at scheduled time using
I wrote a program in C++/OpenGL (using Dev-C++ compiler) for my calculus 2 class.
This is a very basic question for JUnit testing. I wrote a program which
I wrote a test program for testing Cassandra, and I had problems reading data.
I want to write a small program that should print something like testing CPU...
I wrote a program in C with Ubuntu Linux and now I need to
I wrote a program which includes writing and reading from database. When I run
I wrote a program out, which was all in one file, and the methods
I wrote a program that forks some processes with fork(). I want to kill

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.