Hello, ive recently been making a program, and am trying to do some file i/o so that it can log information. Ive been trying to do it via a class. Well, i ran into a problem just clearing the char arrays (for my purposes they need to be cleared to 0 before i can use them). So far i havent found a simple way to do it, char myArray[100] = 0 didnt work. I've been trying to do it via a member function. Heres the class:

class FILEIO
{
      public:
           string fileName;
           char harvest[1000];
           char sendToFile[1000];
           
           // Clears a char array
           void clearCharArray( char arrayToClear[] )
           {
                int arrayLength = 0;
                
                // Get the length of the array passed
                arrayLength = sizeof( arrayToClear );
                
                for( int i = 0; i < arrayLength; i++ )
                {
                     if( arrayToClear[i] != 0 )
                     {
                          arrayToClear[i] = 0;
                     } 
                }
           }
 
};

Im sure im probably missing something simple, but i cant seem to find it. Here is how im implimenting it in my program:

FILEIO testVar;
    
    testVar.harvest[1000];
    
    for( int i = 0; i <= 1000; i++)
    {
         cout << "Test var pos " << i << " is: " << testVar.harvest[i] << endl;
    }
    
    testVar.clearCharArray( testVar.harvest );
    system("PAUSE");
    
    for( int i = 0; i <= 1000; i++)
    {
         cout << "Test var pos " << i << " is: " << testVar.harvest[i] << endl;
    }

Im still new to using classes, incase you havnt noticed

void clearCharArray( char arrayToClear[] )
{
  int arrayLength = 0;

  // Get the length of the array passed
  // this will not give you the length of the array passed
  arrayLength = sizeof( arrayToClear );
  // equivalent to arrayLength = sizeof( char* ) ;

  for( int i = 0; i < arrayLength; i++ )
  {
    if( arrayToClear[i] != 0 )
    {
        arrayToClear[i] = 0;
    }
  }
}

Ah ok that would explain why it isnt changing anything.

Do you know how you would go about finding the length of an array?
Looking around, i found length(); but that only seems to be used with strings.

Thanks

> Do you know how you would go about finding the length of an array?
the array 'decays' into to a pointer when you pass it to a function.
also pass the size of the array (as a second parameter) to the function:

void clearCharArray( char arrayToClear[], std::size_t size )
// ...
testVar.clearCharArray( testVar.harvest, sizeof(testVar.harvest) );

why not you use a std::vector<char> instead?

Yeh, i guess ill just have them pass the length along with the array to clear. I had initially wanted it to find it on its own, but its not worth the trouble. Thanks for your help.

The reason i didnt use std::vector<char> is because ive never heard of it before, and i have no idea what it does. But thanks anyway, im assuming it's some sort of thing that clears arrays/variables? Ill look into it later.

Thanks again


EDIT: Well, it seems to be slightly working, heres the new code i have:

#include <iostream>
#include <string.h>
using namespace std;

// Classes

class FILEIO
{
      public:
           string fileName;
           char harvest[100];
           char sendToFile[100];
           
          // Clears a char array
           void clearCharArray( char arrayToClear[], int valueTo, int lengthOfArray )
           { 
                for( int i = 0; i <= lengthOfArray; i++ )
                {
                     if( arrayToClear[i] != valueTo )
                     {
                          arrayToClear[i] = valueTo;
                     } 
                }
           }
 
};


int main(int nNumberofArgs, char* pszArgs[])
{
    FILEIO testVar;
    
    testVar.harvest[100];
    
    for( int i = 0; i <= 100; i++)
    {
         cout << "Test var pos " << i << " is: " << testVar.harvest[i] << endl;
    }
    
    testVar.clearCharArray( testVar.harvest, 4, 100 );
    system("PAUSE");
    
    for( int i = 0; i <= 100; i++)
    {
         cout << "Test var pos " << i << " is: " << testVar.harvest[i] << endl;
    }
    
    system("PAUSE");
    return 0;

}

When i run this, it does change all the parts of the array to one value (when testing with a lenght of 100), but i have it setting to 4, it changes everything to ♦♦♦♦♦♦ symbols. I dont think this is normal, or is it?

1) To use std::strings in your program use this line:

#include<string>

not this:

#include<string.h>

The latter is a deprecated file for functions to manipulate C style strings.

2) if arrayToClear has capacity of lengthOfArray then the largest valid index for an element of arrayToClear is at lengthOfArray - 1. That means if you do this:

for( int i = 0; i <= lengthOfArray; i++ )
{
    if( arrayToClear[i] != valueTo )

you are reading one past the array since i will no longer be a valid index when i == lengthOfArray

3) arrayToClear is an char and valueTo is an int. It turns out that assigning char an int value from 0-255 is valid since each char is really assigned and int from 0-255 (or maybe it's from 1-256----doesn't really matter) and that's how the computer changes it into a binary number to store in memory. I have no idea what the char is that has an ASCII (the chart that controls the int value of each char in the char set most commonly used in C/C++) value of 4, but it wouldn't surprise me if it was a diamond.

4) If you want to clear an empty C style string all you have to do is assing the element with index of zero the null char, like this:

char word[8] = "hello";
word[0] = '\0';

word is now "empty" or clear when used as a string. If you output the char value of each element of word however, you would get the null char followed by 'e', 'l', 'l', 'o' followed by another null char, followed by 2 junk char since word[6] and word[7] haven't been initialized/assigned any specific value yet. If you are trying to assign every char in an array of char to a specific, default value you can do that too of course, often, though not always, with a loop such as you have attempted.

5) This

testVar.harvest[100];

is meaningless. This:

testVar.harvest;

is a char array of capacity 100. If there is a null char in there somewhere then it is a C style string. If not, then it is a simple array of char.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.