I have text file in D drive named "data.txt". It have several columns and rows like

I want to read it and then convert it to say A=1, B=2, V=3 and then write it so that it will be a array of number. how to do that?

Recommended Answers

All 3 Replies

These instructions assume you want to read it to memory and that the letters are being converted into numbers of equal size (1 byte):

  1. Open the file for reading
  2. Create a string array large enough to store the contents of the file.
  3. Iterate through each character of the file.
  4. Perform a translation using a switch before storing it into the array. Example:

    switch (inchar) {
        case 'A': memarray[i] = '1'; break;
        case 'B': memarray[i] = '2'; break;
        case 'V': memarray[i] = '3'; break;

At that point you can do whatever you want with the string array. If you intended to save it to a new file it would be more efficient to write it directly into the file instead of saving it to memory. If you need to do something more useful than write it to the screen you may consider in your translation stage to organize your data into indexed structures, but since you didn't specify how you wanted to use this data there's no way I could recommend what that structure would be.

commented: I am not still getting it. I am sorry for asking you but can you write the code slightly so that i can have a better idea? +0

N1GHTS has giving you a good hint. That said, we don't do your homework for you... :-(

Member Avatar

I think what he meant is, he is having several columns like he gave an example of.
If you want to do like this A -> 1, B -> 2, C -> 3 ....... Z -> 26, i.e sequentially. Then you should consider subtracting 64 from ASCII value of selected character. As 65 is ASCII of A, hence for A it will give you (65-64)=1. Similarly it will give for others as well.

Be a part of the DaniWeb community

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