Reading txt file in to an char* array.

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  Put the code you need help with here.
#include <iostream>
#include <fstream>
#include <stdlib.h> 

using namespace std;

//A function that will convert a hexadecimal into decimal and returns the integer back to main.
//The Parameters is the array of chars.
int  hexToDecimal(const char* hex);

const int MAX_SIZE = 15;

int main()
{
		ifstream hexa;
		hexa.open("hexNums.txt");
					
			char* hexarray = new char[MAX_SIZE];
						
			while (!hexa.eof())
			{	
				for (int i = 0; i <= MAX_SIZE; i++)
				{

						hexa >> hexarray[i];
						cout << hexarray[i];
				}
			}
			

			delete[] hexarray;   
		
		
		hexa.close();
		
		return 0;
}


I am trying to read this text file: aef301
AA
BA
-6353
-cf
-CF
0
1
-10
11
111111
-111111
CABDF6
-cabdf6
defc
-D

in to the char array. But it comes out as one line and not separated by enters. How would I implement each element of the array to each row in the text file. Please help. THank you.
cout << hexarray[i] << endl;
Last edited on
Well, without the endl; the output is
"aef301AABA-6353-cf-CF01-1011111111-111111CABDF6-cabdf6defc-DDF6-". So with the endl;, i think you know what will happen.

It has to be separated like the ones in the text file. Thank you for replying though. >.<
instead of
hexa >> hexarray[i];
use
hexarray[i] = hexa.get();
Wow! It worked. Can u explain to me why? what does this function do? THank you! Also, with this, will I be able to convert each line or row in to decimal properly? Cause get() seems to just copy the txt file and not organize each row in to an element in to the array. I am not too sure. Thank you.
Last edited on
! row is an array. If you want to hold 20 arrays you need:
1
2
3
4
5
6
char *array[max_rows];
array[0] = new char (MAX);
etc.

Since you are using c++ you could create array of strings for ease of use:
[code]std::string array[MAX]


>> didn't work because it moves formatted data. If you will do:
1
2
char asd[] = "asd1\nasd\n";
cout << asd;

Program won't print \n and \0. In your case end-of-the-line sign was ignored. Get() move unformatted data instead with all signs.
Last edited on
You could change this:
1
2
3
4
5
6
7
8
    while (!hexa.eof())
    {
        for (int i = 0; i <= MAX_SIZE; i++)
        {
            hexa >> hexarray[i];
            cout << hexarray[i];
        }
    }


to this:
1
2
3
4
    while (hexa.getline(hexarray, MAX_SIZE))
    {
        cout << hexarray << endl;                 
    }
Oh wow. getline is really useful. Hmm. I can explore more with this. Thank you. Sorry, but I have one more question. If I want to convert each of these hexidecimals into intergers recursively, what kind of a strategy should I think about? I am currently learning recursive functions and its not really making sense. Thanks again.
You should apply method you find the most fit, not the otherwise.

a41 ..
is
16^0 * 1 + 16^1 * 4 + 16^2 *10 ... etc
So depending on array length you need to do above. Sum of all powers will give you desired number.

To do something recursively you need to find part of algorithm that is repeating itself. Write on sheet of paper what is need to be done to do above counting for some numbers and you will find a way.
The recursion should return to the hexadecimal to decimal conversion where everytime it returns, it increments by 1 so that the 16^x will go to the next character.



1
2
3
4
5

16^n * hexarray[j]; 

return n++;


But how would I find the array length for each element?
I didn't log in in the weekend, so you might have completed it.
Here is what i wrote:

1
2
3
4
5
6
7
8
9
10
11
void chartoint(int *output, char *arr, int power)
{
	*output += (pow(16,(double)power) * charToHex(arr));
 //check if charToHex returns 0xff for errors
	if (power > 0)
	{
		power--;
		arr++;
		chartoint(output, arr, power);
	}
}

some char to hex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
short charToHex(char *c)
{
	switch(*c)
	{
	case '0': return 0;
	case '1': return 1;
	case '2': return 2;
	case '3': return 3;
	case '4': return 4;
	case '5': return 5;
	case '6': return 6;
	case '7': return 7;
	case '8': return 8;
	case '9': return 9;
	case 'a':
	case 'A':
		return 10;
	case 'b':
	case 'B':
		return 11;
	case 'c':
	case 'C':
		return 12;
	case 'd':
	case 'D':
		return 13;
	case 'e':
	case 'E':
		return 14;
	case 'f':
	case 'F':
		return 15;
	default: return 0xff;
	}
}

and example:
1
2
3
4
5
6
7
int output=0;
char arr[] = "af45c";

chartoint(&output, arr, strlen(arr)-1);

cout << output << endl;
cout << hex << output << endl;


ps: bacause of pow, you would need to include math.h
Last edited on
Topic archived. No new replies allowed.