Thread: finding a colon

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    finding a colon

    i have the following code to find the first words of a file....but my words are seperated by ":" it tried to put : along with \n an \0 but it still doesnt find the word it just finds the whole line

    len = strlen(buf[cnt]);
    if (buf[cnt][len-1] == '\n')
    buf[cnt][len-1] = '\0';
    cnt++;

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    You're not testing for a ':' anywhere in that code; why do you expect it to find any colons?
    hello, internet!

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    i did

    i mean i did this
    if (buf[cnt][len-1] == '\n')
    buf[cnt][len-1] = '\0';
    buf[cnt][len-1] = ':';

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I am not sure what you are trying to do, are you trying to parse a line and find the words seperated with a ':' because that code is not going to do the trick. You need to test if the characater you have reached is a ':' and if so do something . And in the fure use code tags.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    36
    i am just trying to get the first words of each line of a file(or an array)....and the lines of the files are seperated by ":"
    example
    a:b:d
    ddd:dfd:dfd

    so that i can get "a" from the first line and "ddd" from the second line

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    read in the line, parse the line, when you have found the first semicolon you know where the word ends. Repeat with next line.

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    36
    i have read the lines......but how to match it with the first occurence of ":"

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    step through the array and check every character to see if its a ':'. If that is not what you mean then Im sorry but you got me confused.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You might want to look at the strchr() function.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I wouldn't recommend the strchr for him. because he wants to parse it more than once and to do that he/she would need to use pointers. Here is a hint
    Code:
    while(there is a line to get){
      counter=0;
       if line[counter] == ':'{
          for(x=0;x<counter;counter++)
              put the line[counter] to stdout
      }
    }
    [edit]on second thought. You could just print until you get a : or a newline or \0. Whatever you want[/edit]
    Last edited by linuxdude; 10-15-2004 at 10:13 AM.

  11. #11
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    Or you could use strtok() and just pass ":" as the delimiter.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How you implement your code is going to depend on what you actually want to do (and want not to do) with your data.

    - strchr()
    will work just fine. Using it, you can find out where the first colon is, therefore giving you the length of the first word. But it doesn't add a \0 where the colon is. This can be good if you don't want to destroy the original array, but bad if you want the use the first word as a string.

    - strtok()
    will work similar to strchr(), but will replace the colon with a \0. The original array is altered, and you can use the first word as a string. This can be either good or bad, depending on what you want.

    - custom code
    you can write your own code to achieve the same results as above.

    Also think about strncpy(), or maybe even these functions.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Obvious answer: It's what your head is in.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Finding primes
    By starripper in forum C++ Programming
    Replies: 19
    Last Post: 01-14-2006, 04:17 PM
  3. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM
  4. another question on using The colon :
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2003, 11:22 AM
  5. single colon
    By kes103 in forum C++ Programming
    Replies: 11
    Last Post: 07-17-2003, 04:21 PM