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.
Printable View
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. ;)
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.
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);
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.Quote:
Originally Posted by ssharish2005
Quzah.
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?
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)
Quote:
Originally Posted by 3saul
If you're getting your text from getenv, I might try something using strchr and memcpy.Quote:
Originally Posted by 3saul
Code:#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"
*/