Solved

Leftover data in strings

Posted on 2004-10-21
189 Views
Last Modified: 2010-04-15
I am writing a C program that is taking input from a file.   Somehow it seems that data is being leftover in certain strings, even after performing a strcpy(string, "");

I may have other questions depending on whether this one gets resolved or not, and if so I will increase the points if anyone cares to answer them.  

What heppens is that both of the strings returned by getLabel and getMnemonic are fine, but the operand string stored by getOperand contains data left over from the previous call.  This code is inside of a while(!feof(inputFile).  When if goes through the first time, say that Length is stored in the operand field.  If the second time, RD is supposed to be stored, it will store it as RDngth with the ngth left over from the last iteration.  Also, if you print out the inputLine received by the function it does NOT contain the ngth, it would end in RD instead.  I have tried many different things including for loops assigning null characters and other print jobs, but nothing seems to work.  Here is the main part of the code that's failing in this way.


j = 0;
        strcpy(inputLine, "");
        fflush(inputFile);
        while ((j < 70) && ((tempChar = fgetc(inputFile)) != '\n') && !feof(inputFile)) {
          inputLine[j] = tempChar;
          j++;
        }//end while
        inputLine[j] = '\0';
        lineNumber++;
        if (lineNumber < 10) {
          fprintf(interFile, "%d    %s\n", lineNumber, inputLine);
        } else {
          fprintf(interFile, "%d   %s\n", lineNumber, inputLine);
        }//end if/else
        fflush(interFile);
        strcpy(label, "");
        strcpy(mnemonic, "");
        strcpy(operand, "");
        gotLabel = getLabel(label, inputLine);
        gotMnemonic = getMnemonic(mnemonic, inputLine);
        gotOperand = getOperand(operand, inputLine);


Here is the getOperand function:
the j is present because the 19th column of the line is where the contents are inside of the file.

int getOperand(char* operand, char *input) {

  int i = 0;
  int j = 18;
  //printf("%s\n", input);
  //fflush(stdout);
  for (i=0; i < 25; i++){
    if(isalnum(input[i+j])) {
      operand[i] = input[i+j];
    } else if(isspace(input[i+j])) {
      operand[i] = '\0';
      return 1;
    } else {
      if (input[i+j] == ',' || ((input[i+j] == '@' || input[i+j] == '#') && i == 0)) {
        operand[i] = input[i+j];
      } else {
        return SPECIAL_CHAR_IN_OPERAND;
      }//end else
    }//end if
  }//end for
}//end function getOperand

Any help would be greatly appreciated.
0
Question by:neverrealm
    9 Comments
     
    LVL 15

    Accepted Solution

    by:
    To avoid this problem, the function should store a null character at the end of the operand string no matter what.  As it is, it only does that if there is whitespace after the operand.  It may be encountering one of the other cases where i gets to 25 or it returns SPECIAL_CHAR_IN_OPERAND.  In those cases, it returns without terminating the string.
    0
     

    Author Comment

    by:neverrealm
    That worked well except that now if the "operand" field is blank in the text file it retains the value of the previous entry.  For instance if 1 iteration had length, and the next iteration has nothing, then length will also be contained in the second iteration
    0
     
    LVL 2

    Expert Comment

    by:Sadrul
    you need to check for the end of the input string.

    change
          for (i=0; i < 25; i++){
    to
          for (i=0; i < 25 && input[i+j]; i++){

    -- Adil
    0
     
    LVL 2

    Expert Comment

    by:Sadrul
    in fact, a better option would be to change

          } else if(isspace(input[i+j])) {

    to
          } else if(isspace(input[i+j]) || input[i+j] == 0) {

    keep the for-loop condition as it is:

          for (i=0; i < 25; i++){

    -- Adil
    0
     

    Author Comment

    by:neverrealm
    That did not fix the problem.  Thank you though.
    0
     
    LVL 2

    Expert Comment

    by:Sadrul
    did you try the second way?

    -- Adil
    0
     

    Author Comment

    by:neverrealm
    I actually hadn't when I wrote that, but then I did and it didn't work, so I didn't make an adjustment
    0
     
    LVL 15

    Expert Comment

    by:efn
    The code that reads in a line guarantees that it will be null-terminated, but not that it will be 19 characters long.  If the terminating null character is before input[18], the getOperand function will be looking at data left over from a previous line.  So it would be a good idea for that function to make sure the input doesn't end before position 18 before it starts processing data at that location.
    0
     
    LVL 23

    Assisted Solution

    by:brettmjohnson
    This certainly seems like a whole lot of work when using fgets(), strchr() or strtok()
    would seem to  be much more appropriate.

    Using fflush() makes your program highly intolerant of I/O redirection.

    strcpy(string, "")  is a very inefficient way to empty a string.  You will commonly
    see  string[0] = '\0'; instead.


    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now