I'm trying to read a single characted from a file using getc() function. Once I reach '/n' characted, I want to process the line.
My problem is that if the first line has more characteds than the next line, the resulting array contains characters from the first line at the end. For example, if my file had the following 2 lines:
abcdef
123
When I process the file, the array will store adcdef as a result of reading the first line and 123def as a result of reading the second line.
I initialize and display th array, after processing each line. It appears to have initialized fine. The moment I more a first character of the next line to the array, the array content displays the first character from line 2, followed by the characters from line 1. In the file example above, it will display 1bcdef
Attached is a code exctract:
char bc_str[30]="";
int bc_char; // stores getc() result which is an integer
int cnt=0;
...
cnt=0;
strcpy(bc_str, "");
while ((bc_char=getc(in_file)) != EOF)
{
if (bc_char != '\n')
{
bc_str[cnt]=bc_char;
printf("bc_str=%s\n", bc_str);
cnt++;
}
else
{ ... process the line
cnt = 0;
strcpy(bc_str, "");
} // end if
} // end while
How can I prevent characters from previous line to appear in my array?
My problem is that if the first line has more characteds than the next line, the resulting array contains characters from the first line at the end. For example, if my file had the following 2 lines:
abcdef
123
When I process the file, the array will store adcdef as a result of reading the first line and 123def as a result of reading the second line.
I initialize and display th array, after processing each line. It appears to have initialized fine. The moment I more a first character of the next line to the array, the array content displays the first character from line 2, followed by the characters from line 1. In the file example above, it will display 1bcdef
Attached is a code exctract:
char bc_str[30]="";
int bc_char; // stores getc() result which is an integer
int cnt=0;
...
cnt=0;
strcpy(bc_str, "");
while ((bc_char=getc(in_file)) != EOF)
{
if (bc_char != '\n')
{
bc_str[cnt]=bc_char;
printf("bc_str=%s\n", bc_str);
cnt++;
}
else
{ ... process the line
cnt = 0;
strcpy(bc_str, "");
} // end if
} // end while
How can I prevent characters from previous line to appear in my array?