Thread: string search

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    string search

    I have a text file that I need to read.

    The text file looks like this:

    CARDATA
    redcars.dat type_1.2-1 speed acceleration inertia
    blackcars.txt type3.4.44-112 weight length wheelbase engine inertia speed

    CONDITION
    good fair poor

    PRICE
    0 25000.0 79000 125000.0 350000


    My questions are:
    1. How do I find a string within a string and store it ( in other words I don't want to store speed twice)?
    2. The lines of text can be variable length so how can I process each line by reading it and finding the text I need? (ex: find the catagory CARDATA and ignore it, but I need redcars.dat and acceleration).

    Thanks for your help!

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    There's a thread that deals with issues that are very much the same as yours. And it's not that far, either. Here's the link :
    http://cboard.cprogramming.com/showthread.php?t=84529
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    also check out these links regarding string manipulation and reading a line from a file:
    http://www.cplusplus.com/ref/cstring/
    http://www.cplusplus.com/ref/cstdio/fgets.html

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    Thanks for the links but I still don't see how these answer the question of finding a string within a string. Can anyone provide an example (code) of searching for a string within a string?
    I appreciate any help.
    Thanks!

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    a string within a string is called a 'substring'. if you DID read the (1st) link i sent, youd have the answer.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Also look up sscanf.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Consider also strcspn.
    Last edited by Dave_Sinkula; 10-24-2006 at 09:10 PM. Reason: Changed link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM