Read data from fstream - fstream

I'm trying to read data from a fstream but this code doesn't work.
It puts 0 to the console.
Can you help me?
// g++ -Wall main.cpp -o main.exe
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::fstream file("main.txt", std::fstream::in | std::fstream::out | std::fstream::app);
file << "45634w6\n";
file << "dtusrjt\n";
while (!file.eof())
{
std::string line;
std::cout << std::getline(file, line) << "\n";
}
int a;
std::cin >> a;
}

Just try this code this code will help you
#include <fstream>
#include <iostream>
#include <string>
//#include <sstream>
using namespace std;
int main()
{
fstream file("main.txt");
file << "45634w6\n";
file << "dtusrjt\n";
file.close();
file.open("main.txt");
string line;
while (!file.fail())
{
getline(file, line);
cout <<line << "\n";
}
int a;
cin >> a;
}

Related

CGAL Parameterization scaling

I have the following code, which matches the example for 90%. I want to "flatten" the 3D model via parameterization. But the input and output do not have the same scale.
The question I have, how can we remain the same scale of the scaling. In the image below you can find the input and the output:
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
//#include <CGAL/Surface_mesh_parameterization/Circular_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Discrete_authalic_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Square_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Barycentric_mapping_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/Error_code.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Surface_mesh<Kernel::Point_3> SurfaceMesh;
typedef boost::graph_traits<SurfaceMesh>::halfedge_descriptor halfedge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<SurfaceMesh>::face_descriptor face_descriptor;
namespace SMP = CGAL::Surface_mesh_parameterization;
int main(int argc, char** argv)
{
std::ifstream in((argc > 1) ? argv[1] : "C:/Users/Niels/Desktop/CGAL/flatten.off");
if (!in) {
std::cerr << "Problem loading the input data" << std::endl;
return EXIT_FAILURE;
}
SurfaceMesh sm;
in >> sm;
// A halfedge on the border
halfedge_descriptor bhd = CGAL::Polygon_mesh_processing::longest_border(sm).first;
// The 2D points of the uv parametrisation will be written into this map
typedef SurfaceMesh::Property_map<vertex_descriptor, Point_2> UV_pmap;
UV_pmap uv_map = sm.add_property_map<vertex_descriptor, Point_2>("v:uv").first;
typedef SMP::Two_vertices_parameterizer_3<SurfaceMesh> Border_parameterizer;
typedef SMP::LSCM_parameterizer_3<SurfaceMesh, Border_parameterizer> Parameterizer;
SMP::Error_code err = SMP::parameterize(sm, Parameterizer(), bhd, uv_map);
if (err != SMP::OK) {
std::cerr << "Error: " << SMP::get_error_message(err) << std::endl;
return EXIT_FAILURE;
}
std::ofstream out("result.off");
SMP::IO::output_uvmap_to_off(sm, bhd, uv_map, out);
return EXIT_SUCCESS;
}
Update, but this runs infinite:
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Surface_mesh_parameterization/IO/File_off.h>
//#include <CGAL/Surface_mesh_parameterization/Circular_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Discrete_authalic_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Square_border_parameterizer_3.h>
//#include <CGAL/Surface_mesh_parameterization/Barycentric_mapping_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/Two_vertices_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/LSCM_parameterizer_3.h>
#include <CGAL/Surface_mesh_parameterization/Error_code.h>
#include <CGAL/Surface_mesh_parameterization/parameterize.h>
#include <CGAL/Polygon_mesh_processing/measure.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
typedef CGAL::Simple_cartesian<double> Kernel;
typedef Kernel::Point_2 Point_2;
typedef Kernel::Point_3 Point_3;
typedef CGAL::Surface_mesh<Kernel::Point_3> SurfaceMesh;
typedef boost::graph_traits<SurfaceMesh>::halfedge_descriptor halfedge_descriptor;
typedef boost::graph_traits<SurfaceMesh>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<SurfaceMesh>::face_descriptor face_descriptor;
namespace SMP = CGAL::Surface_mesh_parameterization;
#include <CGAL/Surface_mesh_parameterization/ARAP_parameterizer_3.h>
int main(int argc, char** argv)
{
std::ifstream in((argc > 1) ? argv[1] : "FlatteningObject.off");
if (!in) {
std::cerr << "Problem loading the input data" << std::endl;
return EXIT_FAILURE;
}
SurfaceMesh sm;
in >> sm;
// A halfedge on the border
halfedge_descriptor bhd = CGAL::Polygon_mesh_processing::longest_border(sm).first;
// The 2D points of the uv parametrisation will be written into this map
typedef SurfaceMesh::Property_map<vertex_descriptor, Point_2> UV_pmap;
UV_pmap uv_map = sm.add_property_map<vertex_descriptor, Point_2>("v:uv").first;
typedef SMP::Two_vertices_parameterizer_3<SurfaceMesh> Border_parameterizer;
typedef SMP::LSCM_parameterizer_3<SurfaceMesh, Border_parameterizer> Parameterizer;
SMP::Error_code err = SMP::parameterize(
sm,
SMP::ARAP_parameterizer_3<SurfaceMesh, Border_parameterizer>(
Border_parameterizer(),
CGAL::Eigen_solver_traits<Eigen::SparseLU<CGAL::Eigen_sparse_matrix<double>::EigenType> >(),
1000,
50
),
bhd,
uv_map);
if (err != SMP::OK) {
std::cerr << "Error: " << SMP::get_error_message(err) << std::endl;
return EXIT_FAILURE;
}
std::ofstream out("result.off");
SMP::IO::output_uvmap_to_off(sm, bhd, uv_map, out);
return EXIT_SUCCESS;
}
I have had some success with using the ARAP method instead. The default 50 iterations was not actually enough and produced a crossed over mesh. Bumping up the iterations to 500 worked.
SMP::Error_code err = SMP::parameterize(
in,
SMP::ARAP_parameterizer_3<geometry::Mesh, Border_parameterizer>(
Border_parameterizer(),
CGAL::Eigen_solver_traits<Eigen::SparseLU<CGAL::Eigen_sparse_matrix<double>::EigenType> >(),
1000,
500),
bhd,
uv_map);
...Using commit bec70a6d52

Undefined reference to 'yylex' in function 'yyparse()'

Every time I try to fix the issue I get this error. I don't know what I'm doing wrong. Here is my yacc file and my lexx file:
HERE IS MY YFILE.Y
%{
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE double
#include "y.tab.h"
extern "C" FILE * yyin;
extern "C" int yylex();
extern int yyparse(void *);
using namespace std;
int yyerror(const char* s);
%}
%token DIGIT CHARACTER OP LEFT_PAR RIGHT_PAR SEMICOLON EQUAL NEWLINE OTHER
%union{
char *s;
}
%%
assignment:
id EQUAL expression SEMICOLON NEWLINE
;
expression:
id OP id '{OP id}'
| id OP id '{'LEFT_PAR'}' '{'expression'}' '{'RIGHT_PAR'}' NEWLINE
| id OP id '{'LEFT_PAR expression'}' '{'RIGHT_PAR'}' NEWLINE
;
id:
CHARACTER
|id CHARACTER
|id DIGIT
;
%%
int yyerror(const char *s)
{
cout <<"error" << s << endl;
return -1;
}
main (){
FILE *yyin = fopen("ex.txt","r");
yyparse();
fclose(yyin);
printf("hi");
return 0;
}
HERE IS MY GRAMMARRULES.L
%{
#include "y.tab.h"
#include <stdio.h>
#define YY_DECL extern "C" int yylex()
using namespace std;
#include <iostream>
void ERROR();
%}
digit [0-9]+
char [a-zA-Z]
op [+\-*/%]
%%
{digit} {return (DIGIT);}
{char} {return (CHARACTER);}
{op} {return (OP);}
"(" {return (LEFT_PAR);}
")" {return (RIGHT_PAR);}
"=" {return (EQUAL);}
";" {return (SEMICOLON);}
"\n" {return (NEWLINE);}
. {return (OTHER);}
%%
void ERROR(){
cout << OTHER <<endl;
}
I have grammar rules and am getting the error undefined reference to 'yylex' in 'yyparse()'
If you're having this issue, both yacc and lex files must be the same name. For example snazzle.l and snazzle.y.

myFile undeclared identifier

I get the following error when i try to solve my file:
myFile undeclared identifier
Help would be appreciated! :) I really dont see what is the problem is in this code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
int main()
{
ofstream myFile;
std::string name;
std::string password;
std::cout << "Enter Name" << std::endl;
std::cin >> name;
std::cout << "Enter password" << std::endl;
std::cin >> password;
std::string info = name + ":" + password;
myFile.open ("Database.txt");
myFile << (info) << std::endl << ;
myFile.close();
Sleep(10000);
}
You sould use : std::ofstream myFile to declare myFile
or you can add using namespace std; at the top of the programm (outside the main) and remove all std::.
If you want to add text to "Database.txt", you sould use :
myFile.open("Database.txt", ios::app);
Something is wrong :
myFile << (info) << std::endl << ;
You sould'nt keep the last <<
You also may want to return something in your main.

counting length of string token using istringstream

I'm getting an error saying no matching function for call to 'getline' involving the getline(tokenizer, " "); Im not sure how to fix this, Ive tried including some other headers but I just keep coming up with more errors.
#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <string>
using namespace std;
char encrypt(char character, int offset);
int main(int argc, char *argv[]) {
ifstream inputFile;
string str;
string token;
bool debug = true;
int lineLength = 0, offset;
inputFile.open(argv[1]);
if (inputFile.fail())
cout << "File failed to open. \n";
istringstream tokenizer(str);
getline(tokenizer, " ");
offset = token.length();
while (getline(inputFile, str)){
lineLength = str.length();
for (int i = 0; i < lineLength; i++)
str.at(i) = encrypt(str.at(i), offset);
cout << str << endl;
}
inputFile.close();
return(0);
}

C++: undefined reference to function - fails if header included and works if source included

I'm not able to make this code work using fellowed convention of header includes.
help.cpp
#include <iostream>
#include "basicMath.h"
using namespace std;
int main() {
int arr[4]={0,1,2,3};
int k=0;
int m=3;
//permutations call
perm(arr,k,m);
return 0;
}
BasicMath.h
#ifndef BASICMATH_H_
#define BASICMATH_H_
template<class T>
void Swap ( T& a, T& b );
template<class T1>
void perm ( T1 arr[], int k, int m);
#endif /* BASICMATH_H_ */
BasicMath.cpp
#include <iostream>
#include "basicMath.h"
using namespace std;
template<class T>
void Swap ( T& a, T& b )
{
T temp;
b=temp;
b=a;
a=temp;
}
template<class T1>
void perm ( T1 arr[], int k, int m)
{
//base case
cout << "Call: " << arr[0] << arr[1] << arr[2] << arr[3] << "\t" << k << "\t" << m << "\n";
if (k==m) {
for (int i=0;i<=m;i++) {
cout << arr[i];
}
cout << endl;
} else {
for (int i=k;i<=m;i++) {
swap(arr[k],arr[i]);
perm(arr,k+1,m);
swap(arr[k],arr[i]);
}
}
}
IF I replace the #include "basicMath.h" by #include "basicMath.cpp" Then program works.
Please help. New to Eclipse Project using headers and src.
Thanks in Adv.
The "simple" answer is that you can't put the implementation of a template function into a .cpp file.
See http://www.parashift.com/c++-faq/templates-defn-vs-decl.html