strings....

This is a discussion on strings.... within the C Programming forums, part of the General Programming Boards category; How can I search a file for a string and then copy it to a string variable?...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question strings....

    How can I search a file for a string and then copy it to a string variable?

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    first, use fread to read into a pretty big buffer, then try something strstr() to search.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: strings....

    Originally posted by flightsimdude
    How can I search a file for a string and then copy it to a string variable?
    fgets()
    strstr()
    strcpy()
    strncpy()
    check out http://www.rt.com/man/ for those.

    Also, this might help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    hammer thanks...I'm almost at wits end...I will look at what you have (i.e., fgets, etc) and try to incorporate it into code to search through a file to look for the string I need. I know I've said it before. I am trying to search through a file that I have opened for reading. I need to search the entire file for occurances of a particular string. I don't know if using a "while" statement with the functions you have mentioned or something similar would accomplish this?
    Thanks!
    Flight

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    hammer or anyone else...

    What would the declaration for the charcter array look like if I want to copy the string(s) into that I find? I will need a char array that can handle up to 20 occurances of a string in a file. Each found string(s) that can be up to 40 characters in length. I wouldn't ask this question if I knew...I really don't know.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157

    Re: hammer or anyone else...

    Originally posted by flightsimdude
    What would the declaration for the charcter array look like if I want to copy the string(s) into that I find? I will need a char array that can handle up to 20 occurances of a string in a file. Each found string(s) that can be up to 40 characters in length. I wouldn't ask this question if I knew...I really don't know.
    char found[20][41];

    20 occurances
    40 characters + NULL

    Copy can look like:
    strcpy(found[3], "This string in 4th location");
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21