The company I'm working for has asked me to find a way to convert an image into Base64. Basically, there is a camera that will be taking pictures in JPG and I need to convert that JPG picture in Base64 so that I can send the data through a PLC program and rebuild it on the application side which is going to be a web app.
I will then have to do :
document.getElementById("ImageLoad").src = "data:image/png;base64," + Bytes;
In Javascript and do the Jquery.
I've tried using the ifstream with ios::in | ios::binary and just reading the file and outputting the result but it doesn't work.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input,line)) {
output << line;
}
input.close();
}
}
I'm expecting an output like the following:
/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAYEBAQFBA
But I'm getting a long string looking like this:
}!× ÙÝÜ÷åXŠmŒš#Õä ‡6gxD1;*wïµ¼4 ÒÑôÿ ¿OÑú\x0¥ˆ‘ÀÃõûóC
This worked for me: https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
I can't believe C++ doesn't have base64 functionality in the standard library!
#include <fstream>
#include <string>
#include "base64.h"
using namespace std;
int main()
{
string line;
ifstream input("test.jpg", ios::in | ios::binary);
ofstream output("text.txt");
if (input.is_open()) {
while (getline(input, line)) {
string encoded = base64_encode(reinterpret_cast<const unsigned char*>(line.c_str()), line.length());
output << encoded;
}
input.close();
}
}
Related
i tried to open and read an application in binary mode and get 100 characters(For a large file, i did this so that i could read all the characters) in binary mode and then transfer them to new file(in fact, this program will be the same as the previous program, but with a different name ) to find out if it works properly or not
so anyway my source code is:
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
//Vector of 100 characters initialised to 0
vector<char> ch(100, 0);
ifstream file("example.exe",ios::in|ios::binary);
if (file.is_open())
{
while (file)
{
file.read(ch.data(), 100);
// Get the number of bytes actually read
size_t count = file.gcount();
ch[file.gcount()] = '\0';
//cout << ch.data() << endl;
ofstream output("output.exe", ios::out | ios::binary | ios::app);
output.write(ch.data(), sizeof(100));
output.close();
}
}
file.close();
}
my problem is the output with the correct information is not included and the size is smaller than the original application(example.exe) what is happening?
The line output.write(ch.data(), sizeof(100));
should be replaced with output.write(ch.data(), count);
Since sizeof(100) only returns 4, which is the size in bytes of an integer.
You should also remove the line ch[file.gcount()] = '\0'; since file.gcount() might be out of bound.
I just tested it and this works for me
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
vector<char> ch(100, 0);
ifstream file("app.exe",ios::in|ios::binary);
if (file.is_open())
{
ofstream output("app_copy.exe", ios::out | ios::binary | ios::trunc);
while (file)
{
file.read(ch.data(), 100);
output.write(ch.data(), file.gcount());
}
output.close();
}
file.close();
}
Notice that ios::app is replaced by ios::trunc, which will delete the content of the file when opening.
How can I open an Excel file in C++ J2ME and get data from it or store data in it?
This is my (miniscule) approach so far:
#include <fstream>
ifstream inFile;
inFile.open("customer.dat");
try this code
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream filestore("customer.dat"); //store data in file
string data1; //store text
int data2; //store integers
cout<<"enter data or press ctrl+z to quit \n";
while(cin>>data1>>data2){
filestore<<data1<<endl;
filestore<<data2<<endl;
}
ifstream fileread("customer.dat");//read data from file
string info1;
int info2;
while(fileread>>info1>>info2){
cout<<info1<<" "<<info2<<endl;
}
return 0;
}
I'm having some problems reading a string into an array. my file contains the following strings running horizontally down the page.
File:
dog
cat
rabbit
mouse
Code:
#include <string>
int i = 0;
using namespace std;
int main()
{
FILE * input1;
fopen_s(&input1, "C:\\Desktop\\test.dat", "r");
string test_string;
while (!feof(input1)) {
fscanf_s(input1, "%s", test_string);
i++;
}
return 0;
}
Any advice would be appreciated, Thanks!
You should use ifstream and std::getline
Now, I'm going to walk you through reading lines from the file using ifstream
Include fstream to use ifstream.
#include <fstream>
Opening a file:
To open a file, create an object of ifstream, and call it's method open and pass the filename as parameter. ifstream opens a file to read from it. (To write in a file, you can use ofstream)
ifstream fin;
fin.open("C:\\Desktop\\test.dat");
Or you can simply pass the filename to the constructor to create an object of ifstream and open a file.
ifstream fin("C:\\Desktop\\test.dat");
Reading from the file:
You can use stream extraction operator (>>) to read from the file, just like you use cin
int a;
fin >> a;
To read a line from a file using the above created fin (using a char array)
char arr[100];
fin.getline(arr, 100);
Better yet, you should use std::string instead or char arrays, using std::string, you can read a line using std::getline
string testString;
getline(fin, testString);
Now, let's change your code to use ifstream and getline
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int i = 0;
ifstream input1;
input1.open("C:\\Desktop\\test.dat");
string test_string;
while (getline(input1, test_string)) {
i++;
}
return 0;
}
I read line by line of a textfile and if a line meets some requirements I want to override the line and save it at the same position in the same file.
Here is what I have (simplified):
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
fstream file;
string line;
file.open("Test.txt");
while (getline(file, line))
{
if (line.size() > 7) file << line.append(" <- long line");
}
}
You can read your file into memory and then write it out after changing any of the lines. The following example reads it into a vector, then writes it back out.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file;
string line;
file.open("Test.txt", fstream::in);
if (file.fail()) exit(-1);
vector<string> vec;
while (getline(file, line, '\n'))
{
string ln = line;
vec.push_back(ln);
}
file.close();
// manipulate your lines here
file.open("Test.txt", fstream::out | fstream::trunc);
for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
file.write(it->c_str(), it->length());
file.write("\n", 1);
}
file.close();
}
But note, when you change a line, the position of lines that follow will change unless the line you are changing is the same size as the original. Also note, this is a simple ANSI file example, but UNICODE and UTF-8 are also common text file formats. This should at least get you started.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc,char *argv){
fstream inout("C:\\Users\\7\\Desktop\\test.txt",ios::in | ios::out | ios::binary);
if (!inout){
cout<<" cannot open input file.\n";
return 1;
}
long e,i,j;
char c1,c2;
e=5;
for (i=0,j=e;i<j;i++,j--){
inout.seekg(i,ios::beg);
inout.get(c1);
inout.seekg(j,ios::beg);
inout.get(c2);
inout.seekp(i,ios::beg);
inout.put(c2);
inout.seekg(j,ios::beg);
inout.put(c1);
}
inout.close();
return 0;
}
why this code writes can't open file
EDIT:
i have made corrections but here is one problem
in test.txt is written such thing
maiko
miyvarxar
shen
me
so it should write
me shen miyvarxar maiko
but it does not write anything
please help
This seems to work for me:
using namespace std;
int main()
{
fstream inout("C:\\Users\\turdfurguson\\Turds\testfile.txt", ios::in | ios::out | ios::binary);
if (inout.good())
cout << "OK!" << endl;
}
Provided you have a "C:\Users\turdfurgson\Turds\testfile.txt" file that is readable and writable.
The code you've provided looks fine.
You may have supplied the wrong path or something like that.
You could also try attempting to open that file in read only mode
and see if that is ok:
std::ifstream in("path", std::ios::binary);
if (!in) {
// fail
}