T
theballz
Hi,
I am learning c programming and come across a problem i cant seem to
solve. I have a file which i wish to parse and put certain lines
(which do not contain a hash character) into an array and then output
the contents of this array. The file seems to be parsed properly and
the array gets populated but when I output the array the last line of
my text file has filled the array. Text file and code as follows,
Text File
======
Comment 1
Comment 2
Comment 3
#Comment 4
#Comment 5
C Code
=====
#include <stdio.h>
int
main()
{
FILE *myfile;
char s[100];
char *myarray[10][1];
int count, count2;
printf("Opening file for reading..\n");
myfile = fopen("samplefile", "rs");
if (!myfile) {
printf("There is no file for reading...");
return(1);
}
count = 0;
printf("Parse file and filter lines into array ...\n\n");
while (fgets(s,100,myfile))
{
if (!strchr(s, '#'))
{
myarray[count][0] = s;
printf("Line %i passed to array: %s", count, myarray[count]
[0]);
count ++;
}
}
fclose(myfile);
printf("\n\nListing array contents..\n\n");
count2 = 0;
while (count2 < count)
{
printf("Line %i array value: %s", count2, myarray[count2][0]);
count2 ++;
}
return (0);
}
When the code is run this is what I see,
Parse file and filter lines into array ...
Line 0 passed to array: Comment 1
Line 1 passed to array: Comment 2
Line 2 passed to array: Comment 3
Listing array contents..
Line 0 array value: #Comment 5
Line 1 array value: #Comment 5
Line 2 array value: #Comment 5
No doubt it is something simple but I seem to have developed a mental
block on this. Hope someone can sort me out.
I am learning c programming and come across a problem i cant seem to
solve. I have a file which i wish to parse and put certain lines
(which do not contain a hash character) into an array and then output
the contents of this array. The file seems to be parsed properly and
the array gets populated but when I output the array the last line of
my text file has filled the array. Text file and code as follows,
Text File
======
Comment 1
Comment 2
Comment 3
#Comment 4
#Comment 5
C Code
=====
#include <stdio.h>
int
main()
{
FILE *myfile;
char s[100];
char *myarray[10][1];
int count, count2;
printf("Opening file for reading..\n");
myfile = fopen("samplefile", "rs");
if (!myfile) {
printf("There is no file for reading...");
return(1);
}
count = 0;
printf("Parse file and filter lines into array ...\n\n");
while (fgets(s,100,myfile))
{
if (!strchr(s, '#'))
{
myarray[count][0] = s;
printf("Line %i passed to array: %s", count, myarray[count]
[0]);
count ++;
}
}
fclose(myfile);
printf("\n\nListing array contents..\n\n");
count2 = 0;
while (count2 < count)
{
printf("Line %i array value: %s", count2, myarray[count2][0]);
count2 ++;
}
return (0);
}
When the code is run this is what I see,
Parse file and filter lines into array ...
Line 0 passed to array: Comment 1
Line 1 passed to array: Comment 2
Line 2 passed to array: Comment 3
Listing array contents..
Line 0 array value: #Comment 5
Line 1 array value: #Comment 5
Line 2 array value: #Comment 5
No doubt it is something simple but I seem to have developed a mental
block on this. Hope someone can sort me out.