P
ptq2238
Hi, I'm getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
The testdata.txt contains
ab$
e.f
1we
@\/
"@'
:]_
..
When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =
count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =
count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =
count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =
count = 16, instring = "
count = 17, instring = @
count = 18, instring = '
count = 19, instring =
Segmentation Fault (core dumped)
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?
Pat
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there's a better method but I'm learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != '\0')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
The testdata.txt contains
ab$
e.f
1we
@\/
"@'
:]_
..
When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =
count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =
count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =
count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =
count = 16, instring = "
count = 17, instring = @
count = 18, instring = '
count = 19, instring =
Segmentation Fault (core dumped)
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?
Pat