-
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++;
-
You're not testing for a ':' anywhere in that code; why do you expect it to find any colons?
-
i did
i mean i did this
if (buf[cnt][len-1] == '\n')
buf[cnt][len-1] = '\0';
buf[cnt][len-1] = ':';
-
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.
-
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
-
read in the line, parse the line, when you have found the first semicolon you know where the word ends. Repeat with next line.
-
i have read the lines......but how to match it with the first occurence of ":"
-
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.
-
You might want to look at the strchr() function.
-
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]
-
Or you could use strtok() and just pass ":" as the delimiter.
-
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.
-
Obvious answer: It's what your head is in. ;)
Quzah.