Thread: Substr position/parameter problem

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    30

    Substr position/parameter problem

    I have just become familiar with substr. I had been using it successfully the past couple days until I ran into this first problem, and now I have begun to question if I actually know how it works. I have some code posted below which reads an input text file and outputs some substrings. For whatever reason, the first parameter of substr (the position part) seems to be off by 3 characters. The line that reads "var_number = line[0].substr(17,11);" seems to function as if the 17 value is really set to 14.

    I read about how to use substr here: substr - C++ Reference. Based upon what I read, I would think I am using substr correctly, but apparently I am not, given the output I am creating. Do you guys see what the problem is?

    The code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line[115];
      int x = 0;
      int i = 0;
      char filename[50];
        
      string var_number;
      string var_time;
        
      cout << "filename equals: ";
      cin >> filename;
      ifstream myfile (filename);
    
      if (myfile.is_open())
      {
        while (myfile.good())
        {
          getline (myfile,line[i]);
          i++;
        }
        myfile.close();
      }
      else cout << "Unable to open file"; 
      
      // LINE 0 VARIABLES
      x = line[0].length();
      // handnumber
      var_number = line[0].substr(17,11);
      // time
      var_time = line[0].substr(x - 11,11);
      
      cout << var_number << endl;
      cout << var_time << endl;
    
      system("pause");
      return 0;
    }
    The input text:
    012345678901234 #54277040317 Blah Blah Blah 20:35:38 ET
    Blah Blah Blah
    Blah Blah Blah
    Blah Blah Blah
    The output text:
    4 #54277040
    20:35:38 ET

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Copied and Pasted into VS2010 and the code works for me.

    EDIT: Some things to try - Delete the object files if they're laying around, check the build process (have you changed the executable's name or directory?), check your input (are you passing in the right filename? are there "duplicates" of the file in other directories? are you opening the correct version?), et cetera
    Last edited by bernt; 12-22-2010 at 03:09 PM.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    I'm using Dev C++. I had heard that it was ancient, maybe it is causing me this problem.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Ditching it for Visual Studio Express, Code::Blocks, or wxDev-Cpp is a good idea whether it's causing it or not.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    Just tried it in VS2010, and had exactly the same output. I am expecting the output to be:

    54277040317
    20:35:38 ET
    and not

    4 #54277040
    20:35:38 ET
    Not likely to just be a problem with my IDE.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So what that apparently means is that there are three characters in front of your line that you don't seem to be expecting. Print line[0] and see what they are.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    30
    You're right. What are those first three characters?

    Attachment 10257

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM