-1

I am trying to get a set of characters I read from a file and store them into an array.

Lets just say I'm pulling each letter from the string MICHAEL out of my inputFile:

int limit = 7;
for(int number = 0; number < limit; number++){
    inputFile >> stepc_char;
    cout << stepc_char << endl;
}

I want to store them into an array. How can I do this?
(Note that I can not use std::vector)

LihO
  • 41,190
  • 11
  • 99
  • 167
Mnramos92
  • 15
  • 1
  • 6

2 Answers2

1
I then want to store them into an array

Okay, you've got the reading part down, as your current code already does read correctly. To store the values into an array:

// (With minimal changes to your code)
char* my_array = new char[limit];
for (int number = 0; number < limit; number++) {
    inputFile >> stepc_char;
    cout << stepc_char << endl;

    my_array[number] = stepc_char;
}

Don't forget to delete[] my_array; when you're done with it though. The only reason I'm allocating the memory like this instead of doing char my_array[limit]; is because the given array size must be constant. (This is what I can tell from my compiler).

If you don't need the array indefinitely, execute delete[] my_array; immediately before you exit scope.

For example, if the above code was inside an if block, this is what it should look like:

if (someExpression) {

    char* my_array = new char[limit];
    for (int number = 0; number < limit; number++) {
        inputFile >> stepc_char;
        cout << stepc_char << endl;

        my_array[number] = stepc_char;
    }

    // process character array

    delete[] my_array;
}
user123
  • 8,970
  • 2
  • 31
  • 52
  • can you explain why you went from my_array[limit]; to my_array[number] – Mnramos92 Mar 21 '13 at 01:33
  • Well, limit appears to be what you're using for the expected character count. number is the int you're using as a loop index. If there are 8 characters in the file, I'd expect limit to be equal to 8, and so, I'd have to create an array of chars having size 8. number starts at 0 and increases by 1 after each iteration in the loop. This means that the first character in the file will be stored inside my_array[0], the second inside my_array[1], so on and so forth until my_array[7] has the last character in the file. – user123 Mar 21 '13 at 01:35
  • 1
    -1 Woah, woah, woah. Dynamic allocation? What for? And even with comment *"With minimal changes to your code"*? I doubt so since this will tie him to the memory management that this array brings with itself. – LihO Mar 21 '13 at 01:37
  • awesome, i am now just confused by this line: char* my_array = new char[limit]; – Mnramos92 Mar 21 '13 at 01:38
  • Yes, yes, dynamic allocation is taking it a bit too far here, but I can't find a better solution other than std::vector which he stated is not suitable for his needs in one of the comments, but I think your std::string solution might do the trick for him. Depends on what the OP's limitations and needs are I guess :v – user123 Mar 21 '13 at 01:53
  • 1
    `delete my_array` should be `delete [] my_array`; the two are not the same (depending on the compiler) – Mr Fooz Mar 21 '13 at 02:01
  • Good catch. Thank you. I really shouldn't be helping people at 4 in the morning ;v – user123 Mar 21 '13 at 02:03
0

Assuming this sequence will not hold a common string (meaning that std::string is not what you are looking for), then std::vector seems to be the most appropriate way of holding this sequence in this case.

But since you added a constraint that you are not allowed to use std::string, then (for the sake of avoiding the taking care of the memory management on your own) I'd suggest variable length array if they are available (i.e. in case you use GCC, see Why aren't variable-length arrays part of the C++ standard?):

char myArray[limit];
for(int i = 0; i < limit; i++)
{
    std::cin >> myArray[i];
}

note that in this case myArray is an array with automatic storage duration whose lifetime ends when the execution goes out of the scope in which it was declared.

Otherwise, you'll have to go with dynamically allocated array:

char* myArray = new char[limit];
for(int i = 0; i < limit; i++)
{
    std::cin >> myArray[i];
}
delete[] myArray;                   // don't forget to clean it up
Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167