Thread: File IO

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    File IO

    Hi guys,

    How can I keep retrieve hex value one by one of a file with the following contents


    53 74 65 67 61 6E 6F 67 72 61 70 68 79 20 52 65 76 65 61 6C 65 64 0D 0A 4B
    72 69 73 74 79 20 57 65 73 74 70 68 61 6C 20 32 30 30 33 2D 30 34 2D 30 39
    0D 0A 4F 76 65 72 20 74 68 65 20 70 61 73 74 20 63 6F 75 70 6C 65 20 6F 66
    20 79 65 61 72 73 2C 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79 20 68 61 73
    20 62 65 65 6E 20 74 68 65 20 73 6F 75 72 63 65 20 6F 66 20 61 20 6C 6F 74
    20 6F 66 20 64 69 73 63 75 73 73 69 6F 6E 2C 20 70 61 72 74 69 63 75 6C 61
    72 6C 79 20 61 73 20 69 74 20 77 61 73 20 73 75 73 70 65 63 74 65 64 20 74
    68 61 74 20 74 65 72 72 6F 72 69 73 74 73 20 63 6F 6E 6E 65 63 74 65 64 20
    77 69 74 68 20 74 68 65 20 53 65 70 74 65 6D 62 65 72 20 31 31 20 61 74 74
    61 63 6B 73 20 6D 69 67 68 74 20 68 61 76 65 20 75 73 65 64 20 69 74 20 66
    6F 72 20 63 6F 76 65 72 74 20 63 6F 6D 6D 75 6E 69 63 61 74 69 6F 6E 73 2E
    20 57 68 69 6C 65 20 6E 6F 20 73 75 63 68 20 63 6F 6E 6E 65 63 74 69 6F 6E
    20 68 61 73 20 62 65 65 6E 20 70 72 6F 76 65 6E 2C 20 74 68 65 20 63 6F 6E
    63 65 72 6E 20 70 6F 69 6E 74 73 20 6F 75 74 20 74 68 65 20 65 66 66 65 63
    74 69 76 65 6E 65 73 73 20 6F 66 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79
    20 61 73 20 61 20 6D 65 61 6E 73 20 6F 66 20 6F 62 73 63 75 72 69 6E 67 20
    64 61 74 61 2E 20 49 6E 64 65 65 64 2C 20 61 6C 6F 6E 67 20 77 69 74 68 20
    65 6E 63 72 79 70 74 69 6F 6E 2C 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79
    20 69 73 20 6F 6E 65 20 6F 66 20 74 68 65 20 66 75 6E 64 61 6D 65 6E 74 61
    6C 20 77 61 79 73 20 62 79 20 77 68 69 63 68 20 64 61 74 61 20 63 61 6E 20
    62 65 20 6B 65 70 74 20 63 6F 6E 66 69 64 65 6E 74 69 61 6C 2E 20 54 68 69
    73 20 61 72 74 69 63 6C 65 20 77 69 6C 6C 20 6F 66 66 65 72 20 61 20 62 72
    69 65 66 20 69 6E 74 72 6F 64 75 63 74 6F 72 79 20 64 69 73 63 75 73 73 69
    6F 6E 20 6F 66 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79 3A 20 77 68 61 74
    20 69 74 20 69 73 2C 20 68 6F 77 20 69 74 20 63 61 6E 20 62 65 20 75 73 65
    64 2C 20 61 6E 64 20 74 68 65 20 74 72 75 65 20 69 6D 70 6C 69 63 61 74 69
    6F 6E 73 20 69 74 20 63 61 6E 20 68 61 76 65 20 6F 6E 20 69 6E 66 6F 72 6D
    61 74 69 6F 6E 20 73 65 63 75 72 69 74 79 2E 0D 0A 0D 0A 57 68 61 74 20 69
    73 20 53 74 65 67 61 6E 6F 67 72 61 70 68 79 3F 0D 0A 0D 0A 57 68 69 6C 65
    20 77 65 20 61 72 65 20 64 69 73 63 75 73 73 69 6E 67 20 69 74 20 69 6E 20
    74 65 72 6D 73 20 6F 66 20 63 6F 6D 70 75 74 65 72 20 73 65 63 75 72 69 74
    79 2C 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79 20 69 73 20 72 65 61 6C 6C
    79 20 6E 6F 74 68 69 6E 67 20 6E 65 77 2C 20 61 73 20 69 74 20 68 61 73 20
    62 65 65 6E 20 61 72 6F 75 6E 64 20 73 69 6E 63 65 20 74 68 65 20 74 69 6D
    65 73 20 6F 66 20 61 6E 63 69 65 6E 74 20 52 6F 6D 65 2E 20 46 6F 72 20 65
    78 61 6D 70 6C 65 2C 20 69 6E 20 61 6E 63 69 65 6E 74 20 52 6F 6D 65 20 61
    6E 64 20 47 72 65 65 63 65 2C 20 74 65 78 74 20 77 61 73 20 74 72 61 64 69
    74 69 6F 6E 61 6C 6C 79 20 77 72 69 74 74 65 6E 20 6F 6E 20 77 61 78 20 74
    68 61 74 20 77 61 73 20 70 6F 75 72 65 64 20 6F 6E 20 74 6F 70 20 6F 66 20
    73 74 6F 6E 65 20 74 61 62 6C 65 74 73 2E 20 49 66 20 74 68 65 20 73 65 6E
    64 65 72 20 6F 66 20 74 68 65 20 69 6E 66 6F 72 6D 61 74 69 6F 6E 20 77 61
    6E 74 65 64 20 74 6F 20 6F 62 73 63 75 72 65 20 74 68 65 20 6D 65 73 73 61
    67 65 20 2D 20 66 6F 72 20 70 75 72 70 6F 73 65 73 20 6F 66 20 6D 69 6C 69
    74 61 72 79 20 69 6E 74 65 6C 6C 69 67 65 6E 63 65 2C 20 66 6F 72 20 69 6E
    73 74 61 6E 63 65 20 2D 20 74 68 65 79 20 77 6F 75 6C 64 20 75 73 65 20 73
    74 65 67 61 6E 6F 67 72 61 70 68 79 3A 20 74 68 65 20 77 61 78 20 77 6F 75
    6C 64 20 62 65 20 73 63 72 61 70 65 64 20 6F 66 66 20 61 6E 64 20 74 68 65
    20 6D 65 73 73 61 67 65 20 77 6F 75 6C 64 20 62 65 20 69 6E 73 63 72 69 62
    65 64 20 6F 72 20 77 72 69 74 74 65 6E 20 64 69 72 65 63 74 6C 79 20 6F 6E
    20 74 68 65 20 74 61 62 6C 65 74 2C 20 77 61 78 20 77 6F 75 6C 64 20 74 68
    65 6E 20 62 65 20 70 6F 75 72 65 64 20 6F 6E 20 74 6F 70 20 6F 66 20 74 68
    65 20 6D 65 73 73 61 67 65 2C 20 74 68 65 72 65 62 79 20 6F 62 73 63 75 72
    69 6E 67 20 6E 6F 74 20 6A 75 73 74 20 69 74 73 20 6D 65 61 6E 69 6E 67 20
    62 75 74 20 69 74 73 20 76 65 72 79 20 65 78 69 73 74 65 6E 63 65 5B 31 5D
    2E 20 0D 0A 41 63 63 6F 72 64 69 6E 67 20 74 6F 20 44 69 63 74 69 6F 6E 61
    72 79 2E 63 6F 6D 2C 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79 20 28 61 6C
    73 6F 20 6B 6E 6F 77 6E 20 61 73 20 22 73 74 65 67 22 20 6F 72 20 22 73 74
    65 67 6F 22 29 20 69 73 20 22 74 68 65 20 61 72 74 20 6F 66 20 77 72 69 74
    69 6E 67 20 69 6E 20 63 69 70 68 65 72 2C 20 6F 72 20 69 6E 20 63 68 61 72
    61 63 74 65 72 73 2C 20 77 68 69 63 68 20 61 72 65 20 6E 6F 74 20 69 6E 74
    65 6C 6C 69 67 69 62 6C 65 20 65 78 63 65 70 74 20 74 6F 20 70 65 72 73 6F
    6E 73 20 77 68 6F 20 68 61 76 65 20 74 68 65 20 6B 65 79 3B 20 63 72 79 70
    74 6F 67 72 61 70 68 79 22 20 5B 32 5D 2E 20 49 6E 20 63 6F 6D 70 75 74 65
    72 20 74 65 72 6D 73 2C 20 73 74 65 67 61 6E 6F 67 72 61 70 68 79 20 68 61
    73 20 65 76 6F 6C 76 65 64 20 69 6E 74 6F 20 74 68 65 20 70 72 61 63 74 69
    63 65 20 6F 66 20 68 69 64 69 6E 67 2

    ? Thanks a lot

    I tried this, it didn't work very well.

    Code:
       char ch[4];
       int t=0;  //extract hex value
       while (!inFile.eof())
       {
    	   inFile.getline(ch,4,' ');
    	   
    	   sscanf(ch, "%x", &t);
    	
        
        
       }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    This would be a simple example:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main() {
        std::ifstream in("hex.txt");
        int num;
        in >> std::hex; // Read in under hex representation.
        while (in >> num)
        {
            std::cout << num << " ";
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That is, unless the file is in binary and you copied the contents from a hex editor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by CornedBee View Post
    That is, unless the file is in binary and you copied the contents from a hex editor.
    Hi,
    my file is in binary *.bin. I open it with notepad but can only see garbage values. How do I extract the array of hex values from binary file as well ?

    Do i really have to find hex editor and copy the values to text file? No other ways?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, that would be a supremely stupid way. I just wondered where the values from your post came from.

    To read a specific byte in a binary file, do this:
    Code:
    std::ifstream ifs(filename, std::ios_base::binary);
    if(!ifs) {
      // Opening failed.
    }
    char b;
    ifs.read(&b, 1);
    This reads the first byte. Calling it again reads the second byte. And so on.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Hi CornedBee,

    thanks for ur reply.

    when I use ur code, some bytes were appended with FFFFF in front. How do I solve such problem?

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What problem?

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by MacGyver View Post
    What problem?
    Instead of getting 53 74 65 67 61,

    I get 53 ffff74 fffff...other values

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Show your code.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    He's casting the char to int, thus sign-extending some values.

    Do:
    Code:
    int widevalue = static_cast<int>(static_cast<unsigned char>(b));
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM