Read Excel file in C++ J2ME and get data from itQ - c++

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;
}

Related

Convert image.jpg to Base64

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();
}
}

How to display a file in c++?

I need someone to edit that code so that i could display a file!!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string data; //enter a data as a string
ifstream datafile; // input datafile as ifstream
datafile.open("test.txt"); // open test file
}
Unfortunately you didn't specify how you want to read and display the file. Thus I made the assumption that output should go to std::cout. In the attached proposal there are two possibilities to read: line-by-line as you would read the file in any text editor or each word separately in a new line (input separated by White spaces, i.e. blanks, Tabulators or line breaks).
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string data; //enter a data as a string
std::ifstream datafile; // input datafile as ifstream
datafile.open("Source.cpp"); // open test file
// read line by line
while (std::getline(datafile, data))
{
std::cout << data << std::endl;
}
/*
// read each word (separated by white spaces)
while (!datafile.eof())
{
datafile >> data;
std::cout << data << std::endl;
}
*/
datafile.close();
return 0;
}
There is no error handling in. If the file does not exist or any other occurs no exceptions will be caught.

Getting Information from input file C++

I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween

Putting a String from a text into an array, using scanf

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;
}

C++ Program help: Structs, arrays, loops, input files, loop not working?

I am trying to write a program which will declare an array of 5 structs from information read from a file. Then I use a loop to the print the information of every element in the array.
The code I have written only seems to read one line from the txt. file. Any tips or advice would be appreciated.
#include <istream>
#include <iostream>
#include <ostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
struct Bankinfo{
string name;
int accountnum;
float checking;
float savings;
string phone;
} bankinfo[5];
int i;
i=0;
cout<<"This is a test program"<<endl;
char x;
x=0;
for (i=0;i<=6;i++)
{
ifstream infile;
char testinfo [10001];
infile.open("testinfo.txt");
cin.get(testinfo,10001);
cout<<testinfo<<endl;
infile>>bankinfo [i].name>>bankinfo [i].accountnum>>bankinfo [i].checking>>bankinfo [i].savings>>bankinfo [i].phone;
cout<<setw(10) << (bankinfo[i].name);
cout<<setw(10) <<(bankinfo [i].accountnum);
cout<<setw(10) <<(bankinfo [i].checking);
cout<<setw(10) <<setprecision (2)<<fixed<<(bankinfo [i].savings);
cout<<setw(15) <<(bankinfo [i].phone);
}
cout<<" "<<endl;
cout<<"Thanks for using the program"<<endl;
return (0);
}
You're opening the file in each iteration of the loop in i. Try to get out of the loop the infile.open(...). Now it will read more lines. I don't see the purpose of that cin.get(...) either.