Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing results of getc in array

Status
Not open for further replies.

artskib

Programmer
Jul 17, 2002
8
US
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?
 
That's why you end strings with '\0'.

BTW, what happens if the line is longer than 29 chars? You don't have any code to prevent buffer overflows...
 
Something like:
Code:
bc_str[cnt]=bc_char;
bc_str[cnt + 1]='\0';

And I agree that the 29 character limit is a poor habit to get into. Give yourself an absurdly large string, like 1024.

Lee
 
Another point is that by doing strcpy(bc_str, "") you are just putting a '\0' in bc_str[0], leaving the rest of the array uncleared. With adding a string terminator as already suggested, this line becomes vitually redundant anyway, but if you really want to clear the array, use something like memset. Your code would look something like...

Code:
char bc_str[30]="";
int bc_char; // stores getc() result which is an integer
int cnt=0;
...
cnt=0;
// Put a 0 in each byte of memory, starting from bc_str[0] for 30 bytes
memset(bc_str, 0, 30);

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
   {
      bc_str[cnt + 1] = '\0';

      ... process the line

      cnt = 0;
      memset(bc_str, 0, 30);
   } // end if
} // end while

Finally, a word of caution about large arrays. Assigning large arrays 'just-in-case' is fine if you have the memory to work with, such practice on something like a PIC will quickly use it all up.


Bertha
 
also assigning absurdly large arrays can lead you to forget that the array does still have a limit. What is absurdly large this week may be just right next week, and two bytes too short the week after...
 
While the criticism of using "an absurdly large array" are valid to a point, what's the alternative? The only thing I can think of would be to use realloc() and free(). The OP's example appears to be more of a homework assignment or learning example, and they probably haven't gotten into dynamic memory allocation yet.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top