开发者

something wrong with my c++ code

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_INBO开发者_StackOverflow中文版UND_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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜