Given the following string 22E:77
I need to copy the characters from the string into another array up until it reaches the : character
I couldn't get this working properly with fgetc. Any help very much appreciated.
Given the following string 22E:77
I need to copy the characters from the string into another array up until it reaches the : character
I couldn't get this working properly with fgetc. Any help very much appreciated.
Feel like posting your code? It'll make it a bit easier for us to see what you've done.
it's ironic considerate rarity patron of love higher knowledge engulfs me...
fgetc() is for reading from files, not arrays. Just use a simple loop to step through the original array checking each character. If the character is a ':' then terimate your new string and break the loop. Otherwise, put that character in the new array.
If you understand what you're doing, you're not learning anything.
Something along these lines is what I need...of course this does not work!
Code:{ const char *name = "LANGUAGE"; int i = 0; char *tmp; tmp = getenv (name); char result[8] = {0x0}; while ( (strchr (tmp[i],":") == NULL) && i != sizeof *tmp); { strcat (result, putc (i, *tmp)); i++; } }
this is one way of doing. there are better ways of doing. the following is a sample code which gives u an idea on how to do it. note i havn't compiled this code.
ssharish2005Code:desname = malloc(strlen(srcname); while(*(srcname+i) != ':') { desname[i] = srcname[i]; i++; } printf("The contents of desname string : %s",desname);
Last edited by ssharish2005; 06-18-2006 at 09:59 PM.
By better ways, you mean ones that actually allocate the right amount of space and will stop correctly? Where's the room for your null character? Oh, and where is your boundry check? You know, for when you don't actually find what you're looking for ... for both questions, actually.Originally Posted by ssharish2005
Quzah.
Hope is the first step on the road to disappointment.
How about this, wrote it up ages ago for reading froma file though
That will search inside a file, i wrote this ages ago, you can easily substitue the reading from the file to the reading of a string...Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char c, *buffer; int n = 0, x, size; FILE *file; file = fopen("a_test_file.txt", "rb"); if(file == NULL) perror("Error opening file!"); else { while(c != EOF) { c = fgetc(file); if(c == ';') { x = ftell(file); } } fseek(file, 0, SEEK_END); size = ftell(file); rewind(file); buffer = (char*) malloc(size-x); if(buffer == NULL) perror("Error allocating memory!"); fseek(file, x, SEEK_SET); fread(buffer, 1, size-x, file); fclose(file); printf("File Size: %i\nByte: %i\nBytes Left: %i\nText After: %s\n", size, x, size-x, buffer); free(buffer); } return 0; }
Hope this helps
ftell() returns a long. Why are you casting malloc()? Why are you stuffing EOF into a char and not an int? Why do you declare a variable that you don't use (n)? Why don't you leave room for the NULL character as Quzah indicated?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Becasue, i used this for a short and simple configuration file for an app i made, so i knew how many bytes it was, what to search for, and such... Plus i just ripped this from it after digging around for it, sorry about that (the n)
Originally Posted by 3saulIf you're getting your text from getenv, I might try something using strchr and memcpy.Originally Posted by 3saulCode:#include <stdio.h> #include <stdlib.h> #include <string.h> char *copy_var(char *dst, size_t size, const char *key, int delimiter) { const char *var = getenv(key); if ( var ) { const char *match = strchr(var, delimiter); printf("var = \"%s\"\n", var); /* DEBUG */ if ( match ) { size_t length = match - var; if ( length >= --size ) { length = size; } memcpy(dst, var, length); dst[length] = '\0'; return dst; } } return NULL; } char *get_language(char *dst, size_t size) { return copy_var(dst, size, "LANGUAGE", ':'); } int main(void) { char language[10]; if ( get_language(language, sizeof language) ) { printf("language = \"%s\"\n", language); } else { puts("not found"); } return 0; } /* my output var = "English:US" language = "English" */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*