Thread: File search by word in C

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    13

    File search by word in C

    Hi all:

    does anyone know how to make a simple function that searches for a particular word in a file? So more words could be inserted at that point of the file?

    Is there anyway to do that?



    Thanks in advance!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no simple way to "insert", or delete for that matter, from the middle of a file. Basicly it comes down to running through the file until you find what you want, copying everything to that point some place, doing your insertion (or deletion, but that takes a slight adjustment on your initial copying, case specific), and then copying the rest of the file over. Try it, it's a good exercise.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    Can anyone make sample code of that ( the above)? ^

  4. #4
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    I think you should quickly edit that last comment from your post....

    Make an effort. Think of what quzah suggested and try to think of exactly what you need to do. Split the problem up into simple tasks. eg:

    1. I need to read from a file.
    2. I have to make changes to a specific part of a file.
    3. I need to write back to a file.

    Post your code after you've made an honest effort. Nobody is here to help do homework, but we are here to help guide you.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    it's not for homework, you dumbASS!!!!

    it's just a "sample code" to guide me.....

    p.s. wat a jackass! maybe his fatass's too big and got stuck in a toilet...
    Last edited by muk_luk; 03-24-2004 at 08:56 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it's not for homework, you dumbASS!!!!
    Then you shouldn't have any problem doing it. We have a high tolerance for ignorance when it comes to students because many teachers are of such poor quality. If this is a real job, or you are teaching yourself then you should know how to begin going about it (for the former) or have enough interest to do research (for the latter).

    >it's just a "sample code" to guide me.....
    Right. Well, if you can't read a file and then use strstr you need more "guiding" than we can offer.

    >p.s. wat a jackass! maybe his fatass's too big and got stuck in a toilet...
    Pointless flames only make you look like a fool. You're post is reported by the way, the mods will be keeping an eye on you.
    My best code is written with the delete key.

  7. #7
    >>the mods will be keeping an eye on you.

    Speak of the devil...

    muk_luk: You're not going to make any friends with that kind of attitude. Quzah expressed your options correctly, and OOPboredom is correct in indicating that you will get much farther here after you've made some kind of attempt on your own. Anything at all.

    I'll give you a pointer in the right direction. Look up fopen and fread/fwrite for an idea on how to open and read/write from/to a file.

    Prelude alreaded pointed you to strstr as a way to parse the data you get from your file. Play with it a bit and come back with whatever you get.

    Good luck (and please, no more flames). Thank you.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    This board offers help to those who want to learn. If you are unwilling to learn, please don't flame people.

    We don't offer complete solutions though we could. There is a simple reason for it: offering complete solutions will teach next to nothing. It will only teach people to come back and expect another complete solution for a problem. We are here to teach, not to provide complete solutions. Some of us do that for a living already.

    If you have tried the above suggestions and have problems with your code or concept, feel free to open up another thread including the part of code you think is the problem. This thread will probably not get you any constructive answers anymore, the people who post here are volunteers, they don't have to help you, so you better make friends not enemies.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    13
    what I was originally intend to do was to simply ask for a piece of sample code so I know what Quah was talking about... (maybe a few lines will do)..

    but some jackass has this problem with typing out a few lines of code for others, thinking he's all good with programming and others ask FOR CODE ONLY BECAUSE THEY ARE DOING HOMEWORK. Well, for his information, SOME PEOPLE DO ACTUALLY WANT TO LEARN ON THEIR OWN.


    Cheers if you want to help..for the rest who don't, go Mod Edit youself!

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Thread closed due to attitude problems of last poster.

    I frankly dont care what the issue is in this case, we dont welcome that kind of abuse here

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Sorry Fordy, just wanted to add, rule 3 discusses bad language and specifically mentions "starring out" letters. If I see it again, you'll get a formal warning.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word search in a text file
    By willie in forum C Programming
    Replies: 3
    Last Post: 11-20-2008, 11:10 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM