I have proble. I have this code, first part with while works I mean good, because by debug code (I print what is save i array, and is looks good). But in for in array inst what i want.
I file I have names in this format:
Peter News
Martin Clear
.
.
.
And my code print in for only last line. I try print other lines in array but in first or last is only last line. I try everthink but it not works.
Here is simple code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *soubor;
char radek[80];
char *data[100];
int pos = 0;
int i;
soubor = fopen("names.txt","r");
while(fscanf(soubor,"%[^\n]", radek) != EOF){
getc(soubor);
data[pos] = radek;
printf("%d radek = %s pole = %s \n", pos, radek, data[pos]); // debug
pos++;
}
for(i=0;i<100;i++){
printf("%s \n",data[i]);
}
return 0;
}
data[pos] = radek;, Its contents will be the last one and the same all Because're just copying the pointer.