Thread: read certain strings from file

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

    read certain strings from file

    Hi...
    i hope u guys can help me......
    im quite newbie and i have a problem...ive ask around but no one knows...
    i want to read a test.mdl and then detect what it say and then process the content....
    this test.mdl file if ur open it will look like text file....
    what i do is im using fgetc(ch) and it doesnt work but other recommended using fgetstring and ive done but there result is not as ive expected....
    it dont want to detect the string....

    this is the example....
    the file content in test.mdl file

    ((procedures first) common test
    ((funtion one)
    (methode 1)
    (methode 2)
    (end function))
    (end procedure))

    i want only to extract

    ((funtion one)
    (methode 1)
    (methode 2)
    (end function))


    from the full system and the result should be like below

    function one
    methode 1
    methode 2
    end function


    the reason is that i can know what and how many methode contain in function one in the test.mdl file...

    im using the fgets function and it dont comes out quit well...it doesnt go to second line and it dont detect the string at all as i want to....the condition is like below
    if (the text) = "function one" then fprintf the whole string until it find the (end function) string
    please help me if u gurus, seniors, have any idea or solution how to code....im using c++, am very glad i anyone can help me on this problem....

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Usually you don't use fgetc() or fgets() in C++, but rather fstreams. Or fprintf for that matter.

    If you're using C-style strings, try strcmp() to compare strings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    tq u so much dwks...yes strcmp and strncmp is a solution but how do we compare just certain string from the whole line in a file....

    eg.
    (function one (procedure two
    (methode three) (methode four) end procedure) end function)


    the progrm runs and the result should be just like below

    function one
    procedure two
    methode three
    methode four
    end proicedure
    end function

    the procedure, function and methode could be anything and may be many methode and function....and the program runs and final result you can extract the function is one, proc is two, there are two methode....in a file

    that is the problem i cant pull the certain word from the line....i can compare and store the procedure but how can i store only the procedure name ( in this case is one). the function name should be store but in another coding and the same goes for the methode....how can we extract and divide them and put them in different file....

    last result

    in procedure file
    one

    in function file
    two

    in methode file
    three
    four

    so i can detect the name from the file...could you help me....it gives me big headaches....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM