Thread: Help in C please.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Help in C please.

    I am current writing a program that will find words in a file and replace those words with other words

    eg. find dog replace it with cat.

    I can open a file fine with (o_fd=open(av[1], O_RDWR))== -1) if blah blah.

    But i was wondering after opening the file, when it reaches the reading state how would i input each of the characters into an array so i could have a while loop to go through the array of characters and if this matches does this does that.


    Code:
    while((n_chars=read(in_fd, buf, BUFFERSIZE))>0)
    {	
         if (n_chars!= 0)
              {
              printf("Read'%d' bytes\n", n_chars);
              }
    }
    I have this code to test if its reading in the file characters.

    I have a char a[Arraysize] declared. I just need to know if i can read the file characters into an array and how to do this.

    - Brez

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What? You are reading them into an array already. What do you think buf is?


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

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    O sorry thanks for telling me lol.

    Yea i see what you mean now.

    But once read into buf how do i use it?

    Is it the same like rough example

    for i=0;i<Arraysize;i++

    if buf[i] = A then remove it and replace etc.

    Do i use buf like that? the same as i would with normal array stuff?

    Thank alot for telling me buf is the array lol my text book has like 1 sentence on what the code done. Since that code was from my text book.

    EDIT - THank you haha im so stupid i just tested it and yea now i will try to do the swapping and stuff. Thank alot for putting me in the right direction lol i sat here for 2 days thinking what to do now i have somewhere to go with this.

    Thank you.
    Last edited by Brez_x; 04-03-2006 at 10:45 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Question: Are all your replacements the same size? Like dog => cat is 3 characters. Anything like replacing bird with mongoose? If so you'll have to move each character from your input buffer into another buffer as you look for the words to replace.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13
    Could he not use "s/wordtoreplace/newword" in regex?

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Mindphuck
    Could he not use "s/wordtoreplace/newword" in regex?
    What does that teach him about array or string manipulation?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Yea waltp i do have to do something like that

    eg. Bird to mongoose

    I was thinking of something like this

    say Get replaces to Grab
    I was thinking of moving everything a array up

    eg.

    G E T_T H I S
    _G E T_T H I S

    So when i replace it will be

    G R A B_T H I S

    I have to go school soon but yea i have a general idea of making strcpy? or something strcpy this array to another array then so on.

    Would this be good? having a for loop and then putting all the array up one every time the program finds "bird" or something.

    I will try this after school and post my code up when i finish. Anyways thank you for bring that to my attention.

Popular pages Recent additions subscribe to a feed