Thread: character array filling with junk

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    15

    character array filling with junk

    This is a tough one for me:

    A long time ago, I made myself a function that allows me to read input from user, validate it in a limited way, and put it into an array. The function:

    Code:
    int cInputString(int iMaxChar, char *cArr, int iValidate)
    {
    /* This is a character input function to limit the length of characters
       inputted by the user.  This is necessary because without using such a 
       function, user could overrun the input stack and cause errors.  It'll 
       accept single-character inputs until it hits the number of characters 
       requested (iMaxChar) or until the user hits Enter. */
    /* iValidate determines whether to check for input.  Data is checked based on
       the following table:
       0: do not check data
       1: verify all data is alpha (lower and upper-case letters)
       2: verify all data is numeric */
    /* int return is the length of the characters inputted */
      fflush(stdin);
      char cC;
      int iCount = 0;
      do
        {
          cC = getch();
          printf ("%c",cC);
          switch (iValidate)
          {
            case 1:
            {
              if((isalpha(cC))||cC==' '||cC==0xd)
              {
                cArr[iCount] = cC;
                ++iCount;
                break;
              }
              else
              {
                printf("\nInvalid Entry, use only letters.  Please try again: ");
                break;
              }
            }
            case 2: 
              {   
              if (atol(&cC)==0 && cC != '0' && cC != 0xd)
                {
                  printf("\nInvalid Entry, use only numbers.  Please try again: ");
                  break;
                }
              else if (cC == 0xd) break;
              else
                {
                  cArr[iCount] = cC;
                  ++iCount;
                  break;
                }
              }
            default:
              {                 
                cArr[iCount] = cC;
                ++iCount;
                break;
              }
          }
        }
      while (iCount < iMaxChar && cC != 0xd);
      printf ("\n");
      fflush(stdin);
      return iCount;
    }
    It works perfectly to get user data:
    Code:
      char cInput[7];
      cInputString(6,cInput,2);
      cInput[7]='\n';
      printf("cInput: %s",cInput);
    the printf shows me exactly what I want to see. Later, I use this to build a file path:
    Code:
      int k;
      char cFilename[17];
      char cPath[4] = "c:\\";
      char cAppend[5] = ".LOG";
      for(k=1;k<=31;k++)
      {
        if(k<10) snprintf(cFilename, sizeof(cFilename), "%s%s0%i%s",cPath,cInput,k,cAppend);
        else snprintf(cFilename, sizeof(cFilename), "%s%s%i%s",cPath,cInput,k,cAppend);
        printf("cInput: %s\n",cInput);
        printf("cFilename: %s\n",cFilename);
      }
    The two printf statements display exactly what I want to see:
    cInput: 201401
    cFilename: c:\20140101.LOG

    so, I open the file:

    Code:
        fp=fopen(cFilename, "r+"); //FILE *fp; was earlier defined outside of the loop
        if(fp == NULL) printf("Read File Error: %s", strerror(errno));
    another printf afterward to display works perfectly.

    I do a bunch of reads from the file, writing to some other files, never touching cInput or cFilename again. At the end of it, before looping again with my for loop, I print it out one last time:

    Code:
        printf("%s done\n\n\n",cFilename);
        printf("cInput: %s\n\n\n",cInput);
        printf("k: %i\n",k);
        printf("cAppend: %s\n",cAppend);
        system("PAUSE");
    Here's what I get:
    Code:
    C:\20140101.LOG done
    
    
    cInput: PE.ALT W33825 2014001 10 C6A ON PB04      C     @   8      E     @
        *** END OF DISPLAY ***+ @    23P043                     +
    
    
    k: 1
    cAppend: .LOG
    Press any key to continue . . .
    Any idea why that would be happening? Is there a better input (not scanf, obviously) that I could be using than the code I wrote for myself?
    Last edited by reillan; 01-30-2014 at 09:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 01-29-2013, 07:58 PM
  2. Replies: 48
    Last Post: 02-12-2012, 02:33 PM
  3. Replies: 3
    Last Post: 10-21-2010, 12:39 PM
  4. Array fills with junk - help
    By Lucky Luke in forum C Programming
    Replies: 1
    Last Post: 12-13-2009, 05:13 PM
  5. Replies: 6
    Last Post: 06-30-2009, 09:37 AM