read from file

This is a discussion on read from file within the C++ Programming forums, part of the General Programming Boards category; My programm reads out the first 2 Numbers of a hpgl plotfile, calculates the distance between those coordinates, and writes ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Question read from file

    My programm reads out the first 2 Numbers of a hpgl plotfile, calculates the distance between those coordinates, and writes acordingly to the distance ones and zeros to another file.
    Two long rows of 1 and 0 are created, like this:
    x: y:
    1 1
    1 1
    1 0
    1 0
    1 0
    1 1
    0 1
    0 1

    Now I would need to collect every 8 digits of either row (x and y)
    to create hex numbers according to the binary pattern of the collected byte.

    Does anyone have an idea how I could readout every 8 digits of either row?
    like
    x: 11111100 = FC
    y: 11000111 = C7

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Well, the easiest way would be to build two strings gradually from each row. Then when you have both strings, convert the binary digits to integers and print them in hexadecimal:
    Code:
    #include <cstdlib>
    #include <cstring>
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int btod(const char *b);
    
    int
    main()
    {
      char     xdigit;
      char     ydigit;
      string   x;
      string   y;
      ifstream in("data.txt");
    
      if (!in) {
        cerr<<"Error opening file"<<endl;
        exit(EXIT_FAILURE);
      }
      while (in>> xdigit >> ydigit) {
        x.push_back(xdigit);
        y.push_back(ydigit);
      }
      cout<<"x: "<< x <<" = "<< hex << btod(x.c_str()) <<endl;
      cout<<"y: "<< y <<" = "<< hex << btod(y.c_str()) <<endl;
    }
    
    int
    btod(
      const char *b
      )
    {
      // You aren't expected to understand this
      return *b ? btod(b + 1) + (*b - '0' << strlen(b) - 1) : 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21