Alright. I'm still having a few problems. I tried to get each line in a separate pointer but it's getting crazy. Here's the code:
Code:
int main ()
{
FILE *fptr;
int i;
printf ("Output from the file");
char s[500];
int count = 0;
fptr = fopen ("file.txt","r");
if (!fptr)
{
return 1;
}
while (fgets(s,1000,fptr) != NULL)
{
if (s[i] == 'P' && s[i+1] == '1')
{
while (s[i] != '\n')
{
count++;
i++;
}
}
for (for i=0; i<count; i++)
{
p1= malloc (sizeof (char)*count);
//printf ("%d", count);
p[i] = s[i];
}
printf ("%s",s);
}
for (i=0 i<count; i++)
{
printf ("%c", p1[i]);
}
fclose(fptr);
return 0;
}
PROBLEM:
The value of the counter in the loop is 4 (4 elements in the first row), and it indeed gies the value of the counter 4 when I printf the counter. But in the following loop:
Code:
for (for i=0; i<count; i++)
{
p1= malloc (sizeof (char)*count);
//printf ("%d", count);
p[i] = s[i];
}
It outputs the counter 40 times. What's going on?