| 696 | } |
| 697 | |
| 698 | int |
| 699 | textToBinary(const char* inputFilename, const char* outputFilename) |
| 700 | { |
| 701 | // |
| 702 | // open the files |
| 703 | // |
| 704 | |
| 705 | ifstream input(inputFilename, ios::in); |
| 706 | ofstream output(outputFilename, ios::out | ios::binary); |
| 707 | if (input.bad()) { |
| 708 | std::cerr << "WARNING - BinaryFileStream - binaryToText()"; |
| 709 | std::cerr << " - could not open file " << inputFilename << std::endl; |
| 710 | output.close(); |
| 711 | return -1; |
| 712 | } |
| 713 | if (output.bad()) { |
| 714 | std::cerr << "WARNING - BinaryFileStream - binaryToText()"; |
| 715 | std::cerr << " - could not open file " << outputFilename << std::endl; |
| 716 | output.close(); |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | // |
| 721 | // until done |
| 722 | // read input consisting doubles till \n and write to output file |
| 723 | // |
| 724 | |
| 725 | char data[100]; |
| 726 | char* dataNext; |
| 727 | double d; |
| 728 | |
| 729 | while (!input.eof()) { |
| 730 | string inputLine; |
| 731 | getline(input, inputLine); |
| 732 | const char* c = inputLine.data(); |
| 733 | const char* cNext = c; |
| 734 | |
| 735 | int loc = 0; |
| 736 | int endLoc = int(inputLine.length()); |
| 737 | int numNumbers = 0; |
| 738 | |
| 739 | while (loc < endLoc) { |
| 740 | |
| 741 | int dataCount = 0; |
| 742 | |
| 743 | |
| 744 | while ((loc < endLoc) && (*cNext != ' ') && (*cNext != '\n')) { |
| 745 | data[dataCount++] = cNext[0]; |
| 746 | cNext++; |
| 747 | loc++; |
| 748 | } |
| 749 | |
| 750 | if (dataCount != 0) { |
| 751 | data[dataCount] = '\n'; |
| 752 | d = strtod(&data[0], &dataNext); |
| 753 | output.write((char*)&d, 8); |
| 754 | numNumbers++; |
| 755 | } |