i have a text file abc.text having 32 characters only.
2b7e151628aed2a6abf7158809cf4f3c

i want to load this file in char *key[16] as below

char *key[16] ={"2b", "7e", "15", "16", "28", "ae", "d2", "a6", "ab",
                   "f7", "15", "88", "09", "cf", "4f", "3c"};

i use the following code

char ch; int i=0;
ifstream F("abc.txt");
while (!F.eof())
{
 F.get(ch);
 strcpy (key[i++] , ch); // this line is erroneous..have no idea here..
}
Member Avatar for jencas

strcpy() is for copying null-terminated C-strings, but you have only a single char(acter).

i have a text file abc.text having 32 characters only.
2b7e151628aed2a6abf7158809cf4f3c

i want to load this file in char *key[16] as below

char *key[16] ={"2b", "7e", "15", "16", "28", "ae", "d2", "a6", "ab",
"f7", "15", "88", "09", "cf", "4f", "3c"};
i use the following code

char ch; int i=0;
ifstream F("abc.txt");
while (!F.eof())
{
F.get(ch);
strcpy (key[i++] , ch); // this line is erroneous..have no idea here..
}

1>
Usage of eof() is inviting problems on your own.Have a look at this.
2>
Its better to use pre-allocated memory for your computations.
3>
The usage of get() as you have used will fetch you a single character.But your objective is to fetch 2 characters and store them as a single string or character array.

Instead of using char pointers as strings you can use character arrays for it.More over if you are sure of the length you want to extract into the array then you can simply do something like this:

char arr[16][3]; //Your array
int i=0;
//Other stuff you need for opening of file and all
while(F.good())
{
arr[i][0]=F.get();
arr[i][1]=F.get();
arr[i][2]='\0';
i++;
}

but my requirement is that i want to upload / populate the
char *key[16].
how will i copy the char arr[16][3] to char * key[16]
strcpy (arr , key ) is not working.....

naeem1973,
Nothing wrong with csurfer's explanation or code. csurfer suggests a best method to you.
If you want to char *key[16] - It means 16 pointers of type char and you have to allocates three bytes char array to represent each string.
Little modification to csurfer's code but this way is not a proper one for you.

char *key[16]; 
int i=0;
//Other stuff you need for opening of file and all
while(F.good())
{
key[i]=new char[3]; 
key[i][0]=F.get();
key[i][1]=F.get();
key[i][2]='\0';
i++;
}
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.