Hi!
I wrote a small code, that reads contents of the file and puts it into multidimensional array. This is the file:
Code:
1:Name1:43
2:Name2:84
3:Name3:53
4:Name4:14
5:Name5:12
6:Name6:63
7:Name7:41
8:Name8:31
9:Name9:51
10:Name10:17
I have two problems:
1. I need to separate each line into 3 variables, the separation character is :. for example I read the line and put values(1, Name1, 43) in 3 different variables. I need something like php explode(); function, but I guess I'll have to write it by myself.. Maybe I could use somehow fscanf to acomplish this?
2. I have no problem reading the first character on each line and casting it as an int, so I could use it's value for adding or dividing, but when I read the last line, it's 10, that take 2 blocks in my array, how can I cast them as an integer!?
Code:
#include <stdlib.h>
#include <stdio.h>
int main() {
char c[60000];
int data[10][400], line_pos=0, nline=0, i=0, ch;
FILE *fd;
fd = fopen("file", "r");
memset(data, 0, sizeof(data));
while((ch=getc(fd)) != EOF){
data[nline][line_pos] = ch;
line_pos++;
if(ch == 0x0a){
line_pos=0;
nline++;
}
}
fclose(fd);
return 0;
Basicly I need to read the first and the last variable from each line of the file and put them into int variable, any suggestions please? thank you!