473,287 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Read bytes from file and then Output to a std::string

SwissProgrammer
220 128KB
Using Windows, CodeBlocks 17.12, GCC 5.1 .

I have a file that I created in C++ and I added a byte order mark to the start of the file.

I read from that file and I am able to read the first byte of that file giving me the first part of that BOM (which I know is there since I put it there).

I would like to place what I read into a std::string to ALSO show up on my CLI output.

Like this:

std::string SS22;
// Get the SS22
// Output the SS22.

I do not just want an output. I want it to be from a std::string.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #ifndef _UNICODE
  2.     #define _UNICODE    // used by Windows headers,
  3. #endif // _UNICODE
  4.  
  5. #ifndef UNICODE
  6.     #define UNICODE     // used by C-runtime/MFC headers.
  7. #endif // UNICODE
  8.  
  9. #include <stdio.h>
  10. #include <iostream>
  11.  
  12. #include <cstddef>
  13. #include <bitset>
  14.  
  15. #include <vector>
  16.  
  17.  
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <fstream>
  21. #include <sstream>
  22. #include <iomanip>
  23.  
  24. #include <string.h>
  25.  
  26. //#include <locale>
  27.  
  28.  
  29.  
  30. using namespace std;
  31.  
  32.  
  33. // Since I am using C++11 and I want to use std::byte
  34. using namespace std;
  35.  
  36.  
  37. // Since I am using C++11 and I want to use std::byte
  38. namespace std
  39.     {
  40.         // define std::byte
  41.         enum class byte : unsigned char {};
  42.     };
  43.  
  44.  
  45. int main()
  46.     {
  47.  
  48.         std::string FileName;
  49.         FileName = "Some_utf8_Text_With_ByteOrderMark.txt";
  50.  
  51.         std::cout << "\n\n\nReading  " << FileName << "\n";
  52.         std::cout << "Contents are: [Byte Order Mark BOM] then [hello - こんにちは - abc]\n";
  53.         std::cout << "The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }\n";
  54.         std::cout << "So, I should see as the first byte: [0xef]\n\n";
  55.  
  56.         const char * CCP_FileName = FileName.c_str();
  57.  
  58.         std::FILE *fp;
  59.  
  60.         unsigned char data;
  61.  
  62.         std::ifstream("VerifiedToHave_utf8_ByteOrderMark03.txt");
  63.  
  64.         fp = std::fopen("VerifiedToHave_utf8_ByteOrderMark03.txt", "r");
  65.  
  66.             fread(&data, 1, 1, fp);
  67.  
  68.         fclose(fp);
  69.  
  70.         //The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }
  71.  
  72.         std::cout << "Now testing to get the Hex of the first byte in two different ways:\n";
  73.  
  74.         std::cout << "   test 1  Hex = [" << std::hex << static_cast<int>(static_cast<unsigned char>(data)) << "]\n";
  75.             // [ef]
  76.         std::cout << "   test 2  Hex = [" << std::hex << static_cast<unsigned int>(static_cast<unsigned char>(data)) << "]\n";
  77.             // [ef]
  78.  
  79.  
  80.         // Hex ef = binary 11101111
  81.         // Hex bb = binary 10111011
  82.         // Hex bf = binary 10111111
  83.  
  84.         std::cout << "Attempting to convert the first byte to binary:\n";
  85.  
  86.  
  87.         std::cout << "   test 3  Binary   = [";
  88.  
  89.         bool BolBit[8];
  90.  
  91.         // fill data
  92.         for(int i = 0; i < 8; i++)
  93.             {
  94.                 BolBit[i] = ((data >> i) & 0x01);
  95.                 std::cout << BolBit[i];
  96.             }
  97.  
  98.         std::cout << "]\nThat should have been [11110111]\n\n";
  99.         // 11110111
  100.  
  101.         std::cout << "Now how do I place that in a std::string?\n\n";
  102.  
  103.  
  104.  
  105. // I tried this but it gives me an error that I have not managed to fix.
  106.  
  107. //        char s24[sizeof( data ) + sizeof( ( char )'\0' )] = { '\0' };
  108. //        // memcpy (void*, const void*, size_t);
  109. //
  110. //        memcpy( s24, data, sizeof( data ) );
  111. //        // error: invalid conversion from 'unsigned char' to 'const void*' [-fpermissive]
  112. //
  113. //
  114. //        puts( s24 );
  115.  
  116.  
  117.         cin.clear();
  118.  
  119.         cout << "Click the Exit button.\n";
  120.  
  121.         string s;
  122.         cin >> s;
  123.  
  124.     }
  125.  


Here is my current output via CLI.



Reading Some_utf8_Text_With_ByteOrderMark.txt
Contents are: [Byte Order Mark BOM] then [hello - πüôπéôπü½πüíπü» - abc]
The BOM is an unsigned char BOM01[3]{ 0xef, 0xbb, 0xbf }
So, I should see as the first byte: [0xef]

Now testing to get the Hex of the first byte in two different ways:
test 1 Hex = [ef]
test 2 Hex = [ef]
Attempting to convert the first byte to binary:
test 3 Binary = [11110111]
That should have been [11110111]

Now how do I place that in a std::string?

Click the Exit button.




I got the ef which is correct.

I got the 11110111 which is correct.

How do I put the ef into a std::string and then output it to the CLI?

How do I put the 11110111 into a std::string and then output it to the CLI?




Thank you.

What am I doing wrong in the code?

What the currect code to use for this?

Thank you.


Later, I plan to convert the second string from a std::string to binary and save it to a new file and see if I can read that new file.


Thank you.
Jul 30 '22 #1
0 9163

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: funkmusha | last post by:
I am trying to read a log file using vb.net. I get an error stating "The process cannot access the file 'C:\test.log' because it is being used by another process." Below is a sample of what I am...
2
by: Tim2Be | last post by:
I have a text file that has a length of 840 bytes. I need to split into 20 bytes starting from byte 1. If there are data in any of the 20 bytes, I display the data. So there are 42 occurences of 20...
2
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
10
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach...
1
by: venkyselvaraj | last post by:
hi ,i want program in c language to read the file "a" in c and the output is aaaaaaabbbbbbb.........ppppppp
28
by: tlpell | last post by:
Hey, read some tips/pointers on PHP.net but can't seem to solve this problem. I have a php page that reads the contents of a file and then displays the last XX lines of the file. Problem is...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.