-
Will this work?
Sorry, but I'm not at a computer with a compiler right now and I want to know if this algorithm will work in my latest program.
Code:
char * base[8] = {0};
char * pBase = 0;
char * buf[4];
char buf2[1024] = {0};
int i;
fgets(buf2, 1024, '\n');
pBase = strtok(buf2, ' ');
for(i = 0; i < 8 && *p != '\0'; i++){
base[i] = *p;
pBase = strtok(NULL, ' ');
}
for(i = 0; i < atoi(base[7]); i++){
fgets(buf[i], 1024, '\n');
}
This code reads a string into a buffer, then parses the tokens in that string into an array of pointers to char. One of the tokens will determine how much of the file to read after parsing the string and that value is used as a condition for a loop to read the rest of the file needed.
The big question is, will this work?
-
Apart from a bunch of syntax errors, and problems with your fgets calls - the algorithm itself seems OK
> fgets(buf[i],
except buf[i] doesn't point to a block of 1024 bytes
And
> atoi(base[7])
had better be < 4, otherwise buf[i] has an out of range subscript problem as well.
-
I can find the syntax errors, but I'm a bit confused by your other comments. I checked my book and found that fgets should be
fgets(string, int, stream);
so would that mean my call would have to be
fgets(buf[i], '\n', stdin); ?
The second argument is what confuses me, is it the number of characters to be read or something of a delimiter?
>And
>> atoi(base[7])
>had better be < 4, otherwise buf[i] has an out of range >subscript problem as well.
You lost me here. base[7] is a pointer to a string. Converted to an integer by atoi it would yield a value that could be used as a condition for the loop. Since the array of pointers is of size 8, making a call for the 7th element should access the last element, right?
Just to double check,
char * buf[4];
is an array of 4 pointers that can be assigned strings?
-
> so would that mean my call would have to be
> fgets(buf[i], '\n', stdin); ?
No,
fgets( buff, sizeof(buff), stdin );
The 2nd parameter is the number of bytes which can be written into the buffer (the 1st parameter), reading from the FILE* stream (the 3rd parameter)
You can't choose the delimiter for fgets - it's always '\n'
> making a call for the 7th element should access the last element, right?
Yeah, that bit is OK - base[7] is the last element of the base array,
But, buf[i] (where you have char * buf[4]) has only 4 elements, so the limit of the loop should be 4
> char * buf[4];
> is an array of 4 pointers that can be assigned strings?
Yes - but you don't assign any strings, and fgets won't do it for you either.
Code:
for(i = 0; i < atoi(base[7]); i++){
buff[i] = malloc( 1024 );
fgets(buf[i], 1024, stdin );
}
-
>But, buf[i] (where you have char * buf[4]) has only 4 elements, >so the limit of the loop should be 4
I see what you mean by that, but the value of base[7] wouldn't be any higher than 4, so I would essentially be saying:
for(i = 0; i < j; i++){
where j is an unknown value in the range of 1 to 4. So after parsing the buffer, the program reads at least one more line and then possibly more according to that value. In the final program I intend to error check this and make sure that the value is within the range of base.
And for an array of pointers to char, I need to allocate memory for every pointer before assigning it, yes? Just the same as I would have to allocate memory for a single pointer to char that I want to point to a string later?
Code:
for(i = 0; i < atoi(base[7]); i++){
buf[i] = malloc(1024); /*Allocates 1024 bytes?*/
fgets(buf[i], sizeof(buf), stdin );
}
-
> I see what you mean by that, but the value of base[7] wouldn't be any higher than 4
That is fine then
> I need to allocate memory for every pointer before assigning it, yes?
Yes.