I am an Android and Java developer and I am not much familiar with C language. As well as you know there is not a String type in C. All I want is getting chars, putting them into an char array and writing these characters as a string. How can I take the whole string which is a array of characters and put it into a variable? This is my code but it does not work properly. The log which I get is:
I/ ( 2234): *********PROPERTY = 180000€¾Ü €¾Ü €¾
It should have been 180000.
int c;
char output[1000];
int count = 0;
char *property;
FILE *file;
file = fopen("/cache/lifetime.txt", "r");
LOGI("****************FILE OPEN*************");
if (file) {
LOGI("*****************FILE OPENED************");
while ((c = getc(file)) != EOF) {
putchar(c);
output[count] = c;
++count;
LOGI("******C = %c", c);
}
property = output;
LOGI("*********PROPERTY = %s", property);
fclose(file);
}
What you are missing is a
'\0'. All strings in C are just a sequence of characters ending with a'\0'.So, once your loop
is done, you can add the statement
Below modifications are needed if you intend to return the
propertyvariable outside the local function and ifoutputis a variable local to the function.In the above below line will need modification
You should allocate memory for property using malloc and then use strcpy to copy the string in output to property or do a strdup as suggested by Joachim in the comment.
Using strdup, the statement will be like below