Thread: beginning question

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    beginning question

    I don't know much C++. I did mostly java but I'm trying to write a program in C++. The idea is simple, it looks at a text file and makes a new textfile from some data in the original. That part works fine. Now I need to break up each line of this new textfile into individual parts. In java I'd use substring to do this but in C++ it doesn't quite work the way I want. I'm trying to print a name on the line. The name always starts 6 characters in, and is terminated by the first occurence of "</td>". It doesn't do what I want it and prints random amounts of characters. Here's what I have:

    Code:
    ifstream myread2("goto.txt");
    ofstream myorder("order.txt");
      while(myread2.is_open() && line.compare("-")!=0)
      {
          getline (myread2,line);
          string name;
          string pos;
          double points;
          int e =line.find_first_of("</td>",5);
          name = line.substr(6,e);
          cout << name << endl;
      }
      myread2.close();

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should check the return value of find against string::npos to make sure it found something. Also, find_first_of finds the first character in that string. You probably want find.

    On a separate note, you should probably replace the is_open call in the while control with the call to getline. You want to check the return value of getline before you use the string it gets. It will evaluate to false if it failed to get any data. Just check is_open separately before the loop.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You actually have two problems there.

    http://www.cplusplus.com/reference/s..._first_of.html

    http://www.cplusplus.com/reference/s...ring/find.html

    find_first_of find the first of any of those characters in ths string. I am guessing this is not you intention. You probably just want find.

    http://www.cplusplus.com/reference/s...ng/substr.html

    C++ substr doesn't go between two positions, it starts at the first position then goes for the length.

    Example:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
    	std::string myString = "This Is A String Check It Out";
    
    	size_t startPos = myString.find("String");
    
    	if(startPos == std::string::npos){
    		return 0;
    	}//if
    	
    	size_t endPos = myString.find(" ", startPos);
    
    	if(endPos == std::string::npos){
    		return 0;
    	}//if
    
    	std::string myStringSub = myString.substr(startPos, endPos - startPos);
    
    	std::cout<<myStringSub;
    
    	std::cin.get();	
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Well, it works now. I didn't really understand alot of what you guys said but I did use find instead of find_first. I'm still not sure what substring is doing right now but its working. betweenNames has a </td> in it and it finds the first occurrence and returns the index at the end of it, so I have to substract the length of </td> - 1, and it seems to get the text from 6 to there.

    Code:
    while(myread2.is_open() && !myread2.eof())
      {
    	  getline (myread2,line);
    	  string name;
    	  string pos;
    	  double points;
    	  int endname =line.find(betweenNames);
    	  name = line.substr(6,endname-6);
    	  cout << name << endl;
      }

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Daved was saying to do this instead of what you have now because this will fail upon reading the data in, breaking the loop.
    Code:
    while(getline (myread2,line))
      {  
    	  string name;
    	  string pos;
    	  double points;
    	  int endname =line.find(betweenNames);
    	  name = line.substr(6,endname-6);
    	  cout << name << endl;
      }
    And you seem to have gotten what I said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. beginning array question
    By Noobie in forum C Programming
    Replies: 22
    Last Post: 05-01-2003, 03:18 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM