how to read a character array line by line in C?
-
i have a character arry in C hello hi soidjosdc kcopsdkcpok cjsdp dskcoksdpc ocsdkcpd i want to read it line by line and store it in another array....please help
-
Okay, I'll byte.. In essence, you have a string that you have to cut up into smaller pieces. The cut-points are clearly defined - the string is to be cut at the end of each line of text. So, we need 1) knowledge of the number of lines 2) memory to hold each of the lines 3) to copy each of the lines to the memory allocated in step 2. Sooo, you'll need to know what your line-ending character(or sequence) is. You'll need to count the number of instances of the of line terminators, and for ease and simplicity, add 1 to this number - this is the number of lines you have - mLineCount. You can then create an array of mLineCount elements of char* You can then copy each line, storing the address of the new text into the appropriate array element. Two functions you'll likely make use of are strdup and strtok. I advise you read the documentation for these two functions carefully, there are a few gotchas or things to watch for with strtok - Particularly, the fact that it modifies the input string. This precludes the use of the function on const char* strings. For example, while the intention below is to split "some text" into two separate strings, it will fail since strtok can't modify myString.
char *myString = "some text", *delims = ",. ";
char *curToken;curToken = strtok(myString, delims);
-
i have a character arry in C hello hi soidjosdc kcopsdkcpok cjsdp dskcoksdpc ocsdkcpd i want to read it line by line and store it in another array....please help
I and others already answered a similar query below. If you still don't understand string parsing then you need to spend a lot of time reading the MSDN documentation and sample code on the functions I recommended to you.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
i have a character arry in C hello hi soidjosdc kcopsdkcpok cjsdp dskcoksdpc ocsdkcpd i want to read it line by line and store it in another array....please help
You may hand-craft it, for instance:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>int main()
{
int n, count;
int item, lastindex;
char ** b;
const char a[] = "hello hi soidjosdc\nkcopsdkcpok cjsdp\ndskcoksdpc ocsdkcpd";// step 1: count newlines
const int LEN = sizeof(a);for (n=0, count = 0; n<LEN; n++)
{
if (a[n] =='\n' || a[n] == '\0')
count++;
}// step 2: create the new array
b = (char **) malloc( count * sizeof(char *));
assert(b);// step 3: populate the array
for (n=0, item=0, lastindex = 0; n<LEN; n++)
{
if (a[n] =='\n' || a[n]=='\0')
{
int k, len;len = n-lastindex; b\[item\] = (char \*) malloc(len+1); assert(b\[item\]); for (k=0; k<len;k++) { b\[item\]\[k\] = a\[lastindex+k\]; } b\[item\]\[k\] = '\\0'; lastindex = n+1; item++; }
}
// step 4: use the new array
for (item=0; item<count; item++)
{
printf("%s\n", b[item]);
}// step 5: free the allocated memory
for (item=0; item<count; item++)
{
free(b[item]);
}
free(b);return 0;
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You may hand-craft it, for instance:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>int main()
{
int n, count;
int item, lastindex;
char ** b;
const char a[] = "hello hi soidjosdc\nkcopsdkcpok cjsdp\ndskcoksdpc ocsdkcpd";// step 1: count newlines
const int LEN = sizeof(a);for (n=0, count = 0; n<LEN; n++)
{
if (a[n] =='\n' || a[n] == '\0')
count++;
}// step 2: create the new array
b = (char **) malloc( count * sizeof(char *));
assert(b);// step 3: populate the array
for (n=0, item=0, lastindex = 0; n<LEN; n++)
{
if (a[n] =='\n' || a[n]=='\0')
{
int k, len;len = n-lastindex; b\[item\] = (char \*) malloc(len+1); assert(b\[item\]); for (k=0; k<len;k++) { b\[item\]\[k\] = a\[lastindex+k\]; } b\[item\]\[k\] = '\\0'; lastindex = n+1; item++; }
}
// step 4: use the new array
for (item=0; item<count; item++)
{
printf("%s\n", b[item]);
}// step 5: free the allocated memory
for (item=0; item<count; item++)
{
free(b[item]);
}
free(b);return 0;
}If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
That's what I'm talkin' about. I love your cruel avoidance of the simplifying (though functionality hiding) strdup and strtok. :laugh: :thumbsup:
enhzflep wrote:
That's what I'm talkin' about.
In fact I just spoon-fed the OP with your method.
enhzflep wrote:
I love your cruel avoidance of the simplifying (though functionality hiding) strdup and strtok.
Yes, that's my own departure: why should we use a function when we've the 'metal'? :laugh:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]