I am making a simple compiler, and I use flex and a hashtable (unordered_set) to check if an input word is an identifier or a keyword.
%{
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unordered_set>
using std::unordered_set;
void yyerror(char*);
int yyparse(void);
typedef unordered_set<const char*> cstrset;
const cstrset keywords = {"and", "bool", "class"};
%}
%%
[ \t\n\r\f] ;
[a-z][a-zA-Z0-9_]* { if (keywords.count(yytext) > 0)
printf("%s", yytext);
else
printf("object-identifier"); };
%%
void yyerror(char* str) {printf("ERROR: Could not parse!\n");}
int yywrap() {}
int main(int argc, char** argv)
{
if (argc != 2) {printf("no input file");}
FILE* file = fopen(argv[1], "r");
if (file == NULL) {printf("couldn't open file");}
yyin = file;
yylex();
fclose(file);
return 0;
}
I tried with an input file that has only the word "class" written, and the output is object_identifier, not class.
I tried with a simple program, without using flex and the unordered_set works fine.
int main()
{
cstrset keywords = {"and", "class"};
const char* str = "class";
if (keywords.count(str) > 0)
printf("works");
return 0;
}
What could be the problem?
Use unordered_set<string> instead of your unordered_set<const char*>. You are trying to find the pointer to the char array that obviously cannot exist inside your defined variable.
Related
I'm trying to work with a database in C++. I made a program that opens the database and then creates the tables in procedural programming.
When trying to make it in OOP, sqlite3_exec() != SQLITE_OK
I am new at this, so be gentle.
Here is main.cpp:
#include <iostream>
#include "sqlite3.h"
#include "Table.h"
using namespace std;
int openDatabase(sqlite3 *db);
int main() {
sqlite3 *db;
string columnValues, rowValues; // these are for query
Table Personal;
Personal.SettableName("PERSONAL");
columnValues = "NUME TEXT, ID TEXT"; // this is just an example
openDatabase(db);
Personal.createTable(db, columnValues);
sqlite3_close(db);
return 0;
}
Table.cpp
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Table.h"
#include "sqlite3.h"
using namespace std;
static int callback(void *data, int argc, char **argv, char **azColName) {
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
void executeSqlStatement(sqlite3 *db,const char* sql) {
int rc = 0 ;
char *zErrMsg = 0;
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); // Here it doesn't work, rc=21;
if( rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else {
fprintf(stdout, "Operation done successfully\n");
}
}
void Table::createTable(sqlite3 *db, string columnDetails) {
this->sqlCommand = "CREATE TABLE ";
this->sqlCommand += (this->tableName + " (" + columnDetails + ");");
executeSqlStatement(db, this->sqlCommand.c_str());
printf(sqlCommand.c_str());
}
And Table.h
#include "sqlite3.h"
using namespace std;
void executeSqlStatement(sqlite3 *db);
static int callback(void *data, int argc, char **argv, char **azColName);
class Table
{
public:
void SettableName(string val){tableName = val;}
void createTable(sqlite3 *db, string columnDetails);
string tableName;
string sqlCommand;
};
Including the sqlite3.h file in all three files is not necessary, as Table.h was included by both the cpp files, so you only needed to have it in that file.
The sqlite3.h file is also a system file, so using include <sqlite3.h> instead of include "sqlite3.h" makes it clearer where the file is coming from.
I would recommend compiling with the -Wall and -Wextra flags - at first they appear to make loads of complaints but it is worth paying attention to the problems reported and working out how to fix them.
Table.h
#include <sqlite3.h>
using namespace std;
void executeSqlStatement(sqlite3 *db);
int callback(void *data, int argc, char **argv, char **azColName);
class Table
{
public:
void SettableName(string val){tableName = val;}
void createTable(sqlite3 *db, string columnDetails);
string tableName;
string sqlCommand;
};
Table.cpp
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Table.h"
using namespace std;
int callback(void *data, int argc, char **argv, char **azColName) {
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i = 0; i < argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
void executeSqlStatement(sqlite3 *db,const char* sql) {
int rc = 0 ;
char *zErrMsg = 0;
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); // Here it doesn't work, rc=21;
if( rc != SQLITE_OK ) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else {
fprintf(stdout, "Operation done successfully\n");
}
}
void Table::createTable(sqlite3 *db, string columnDetails) {
this->sqlCommand = "CREATE TABLE ";
this->sqlCommand += (this->tableName + " (" + columnDetails + ");");
executeSqlStatement(db, this->sqlCommand.c_str());
printf(sqlCommand.c_str());
}
main.cpp
#include <iostream>
#include "Table.h"
using namespace std;
int main() {
sqlite3 *db;
sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
string columnValues, rowValues; // these are for query
Table Personal;
Personal.SettableName("PERSONAL");
columnValues = "NUME TEXT, ID TEXT"; // this is just an example
Personal.createTable(db, columnValues);
sqlite3_close(db);
return 0;
}
On my github I have a working version of your code, with a (borrowed) makefile to pull the files together so I just have to type make debug to compile the code.
make debug && bin/debug/hello
will build the file, and if the compilation was successful run the executable.
How do I copy a file from one folder to another folder using C++?
This should be the minimal code required:
#include <fstream>
// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
std::ifstream src(SRC, std::ios::binary);
std::ofstream dest(DEST, std::ios::binary);
dest << src.rdbuf();
return src && dest;
}
int main(int argc, char *argv[])
{
return copyFile(argv[1], argv[2]) ? 0 : 1;
}
it glosses around some potentially complicated issues: error handling, filename character encodings... but could give you a start.
With std::filesystem::copy_file from C++17:
#include <exception>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path sourceFile = "path/to/sourceFile.ext";
fs::path targetParent = "path/to/target";
auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
try // If you want to avoid exception handling, then use the error code overload of the following functions.
{
fs::create_directories(targetParent); // Recursively create target directory if not existing.
fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
}
catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
{
std::cout << e.what();
}
}
I've used std::filesystem::path::filename to retrieve the source filename without having to type it manually. However, with std::filesystem::copy you can omit passing the filename to the target path at all:
fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
Change the behaviour of both functions with std::filesystem::copy_options.
If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().
Here's a previous question covering copy_file():
How to use copy_file in boost::filesystem?
This is how you can do this.
include c++ library <windows.h>
Use function
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
All Done :)
The code below will copy all the file from one directory to another.
Its working code in C++
#include <windows.h>
/*
BOOL Copy(char r_szPath[1024], char r_szDir[1024])
{
char l_szTemp[2048] = {0};
sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
if(IsDirectory(
}*/
#include <stdio.h>
#include<conio.h>
BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[1025] = {0};
memcpy(l_szTmp,r_szSrcPath,1024);
char l_szSrcPath[1025] = {0};
char l_szDesPath[1025] = {0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
char l_szNewSrcPath[1025] = {0};
char l_szNewDesPath[1025] = {0};
strcat(l_szTmp,"*");
hFind = FindFirstFile(l_szTmp, &FindFileData);
if(hFind == NULL) return FALSE;
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName,"."))
{
if(strcmp(FindFileData.cFileName,".."))
{
printf ("The Directory found is %s<BR>, FindFileData.cFileName);
sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__Copy(l_szNewSrcPath,l_szNewDesPath);
}
}
}
else
{
printf ("The File found is %s<BR>, FindFileData.cFileName);
char l_szSrcFile[1025] = {0};
char l_szDesFile[1025] = {0};
sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
}
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return TRUE;
}
int main(int argc, char *argv[])
{
__Copy("C:\fcdb\","E:\sandy\");
getch();
return 0;
}
I want to make a program that reads the highest value from one file and stores it in another. I've read about ifstream and ofstream but how do I let the ofstream store the highest value from the instream in another file? Here is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct CsvWhitespace : ctype<char> {
static const mask* make_table() {
static vector<mask> v{classic_table(), classic_table() + table_size};
v[','] |= space; // comma will be classified as whitespace
return v.data();
}
CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
} csvWhitespace;
int main() {
string line;
ifstream myfile ("C:/Users/Username/Desktop/log.csv");
ofstream myfile2 ("C:/Users/Username/Desktop/log2.csv");
return 0;
}
auto v = vector<int>{};
myfile.imbue(locale{myfile.getloc(), &csvWhitespace});
copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
myfile2 << *max_element(begin(v), end(v));
}
Thanks in advance :)
You could just copy from the one file in the other, without having to worry about the format, by treating them in binary mode. Here is an example:
#include <stdio.h>
#include <string.h>
#define bufSize 1024
int main(int argc, char *argv[])
{
FILE *ifp, *ofp;
char buf[bufSize];
if (argc != 3)
{
fprintf(stderr,
"Usage: %s <soure-file> <target-file>\n", argv[0]);
return 1;
}
if ((ifp = fopen(argv[1], "rb")) == NULL)
{ /* Open source file. */
perror("fopen source-file");
return 1;
}
if ((ofp = fopen(argv[2], "wb")) == NULL)
{ /* Open target file. */
perror("fopen target-file");
return 1;
}
while (fgets(buf, sizeof(buf), ifp) != NULL)
{ /* While we don't reach the end of source. */
/* Read characters from source file to fill buffer. */
/* Write characters read to target file. */
fwrite(buf, sizeof(char), strlen(buf), ofp);
}
fclose(ifp);
fclose(ofp);
return 0;
}
which was given as an example in IP, source. You just need to specify the cmd arguments as the desired files.
You can do it like this. Live example using cin and cout rather than files.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct CsvWhitespace : ctype<char> {
static const mask* make_table() {
static vector<mask> v{classic_table(), classic_table() + table_size};
v[','] |= space; // comma will be classified as whitespace
return v.data();
}
CsvWhitespace(size_t refs = 0) : ctype{make_table(), false, refs} {}
};
int main() {
string line;
ifstream myfile("log.csv");
ofstream myfile2("log2.csv");
auto v = vector<int>{};
myfile.imbue(locale{myfile.getloc(), new CsvWhitespace{}});
copy(istream_iterator<int>{myfile}, istream_iterator<int>{}, back_inserter(v));
myfile2 << *max_element(begin(v), end(v));
}
I have two FASTA files:
file1.fasta
>foo
ATCGGGG
>bar
CCCCCC
file2.fasta
>qux
ATCGGAAA
What I want to do now is to concatenating them into one file that results:
>foo
ATCGGGG
>bar
CCCCCC
>qux
ATCGGAAA
Thus preserving the name of each sequence that started with ">".
Currently my code below replace that name with index, namely:
>0
ATCGGGG
>1
CCCCCC
>0
ATCGGAAA
What's the right way to modify my code below?
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include<stdio.h>
#include<string>
using namespace std;
#define MAX_LINE_SIZE 1024
int mk_joint_file(char *ctrlFile, char *tgtFile, char *outFile){
char s[MAX_LINE_SIZE];
FILE *ofp = fopen(outFile,"w");
FILE *cfp = fopen(ctrlFile,"r");
FILE *tfp = fopen(tgtFile,"r");
// char *p;
int flg=false;
int line=0;
while(fgets(s,MAX_LINE_SIZE,cfp) != NULL){
if(s[0]=='>'){
flg=true;
fprintf(ofp,">%d\n",line);
line++;
}else{
if(flg==true){
fprintf(ofp,"%s",s);
}
flg=false;
}
}
flg=false;
line=0;
while(fgets(s,MAX_LINE_SIZE,tfp) != NULL){
if(s[0]=='>'){
flg=true;
fprintf(ofp,">%d\n",line);
line++;
}else{
if(flg==true)
fprintf(ofp,"%s",s);
flg=false;
}
}
fclose(cfp);
fclose(tfp);
fclose(ofp);
return(0);
}
int main(int argc, char **argv)
{
string ifname_control = argv[1];
string ifname_target = argv[2];
string ofname = "newjoin.txt";
mk_joint_file((char *)ifname_control.c_str(), (char *)ifname_target.c_str(), (char *)ofname.c_str());
}
Is it any harder than just changing these lines
fprintf(ofp,">%d\n",line);
to
// TODO check fgets() handling of EOL - may not need the \n
fprintf(ofp, %s\n", s);
just change line 29 and 40 to
fprintf(ofp,"%s",s);
How do I copy a file from one folder to another folder using C++?
This should be the minimal code required:
#include <fstream>
// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
std::ifstream src(SRC, std::ios::binary);
std::ofstream dest(DEST, std::ios::binary);
dest << src.rdbuf();
return src && dest;
}
int main(int argc, char *argv[])
{
return copyFile(argv[1], argv[2]) ? 0 : 1;
}
it glosses around some potentially complicated issues: error handling, filename character encodings... but could give you a start.
With std::filesystem::copy_file from C++17:
#include <exception>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path sourceFile = "path/to/sourceFile.ext";
fs::path targetParent = "path/to/target";
auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
try // If you want to avoid exception handling, then use the error code overload of the following functions.
{
fs::create_directories(targetParent); // Recursively create target directory if not existing.
fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
}
catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
{
std::cout << e.what();
}
}
I've used std::filesystem::path::filename to retrieve the source filename without having to type it manually. However, with std::filesystem::copy you can omit passing the filename to the target path at all:
fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
Change the behaviour of both functions with std::filesystem::copy_options.
If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().
Here's a previous question covering copy_file():
How to use copy_file in boost::filesystem?
This is how you can do this.
include c++ library <windows.h>
Use function
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
All Done :)
The code below will copy all the file from one directory to another.
Its working code in C++
#include <windows.h>
/*
BOOL Copy(char r_szPath[1024], char r_szDir[1024])
{
char l_szTemp[2048] = {0};
sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
if(IsDirectory(
}*/
#include <stdio.h>
#include<conio.h>
BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[1025] = {0};
memcpy(l_szTmp,r_szSrcPath,1024);
char l_szSrcPath[1025] = {0};
char l_szDesPath[1025] = {0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
char l_szNewSrcPath[1025] = {0};
char l_szNewDesPath[1025] = {0};
strcat(l_szTmp,"*");
hFind = FindFirstFile(l_szTmp, &FindFileData);
if(hFind == NULL) return FALSE;
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName,"."))
{
if(strcmp(FindFileData.cFileName,".."))
{
printf ("The Directory found is %s<BR>, FindFileData.cFileName);
sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__Copy(l_szNewSrcPath,l_szNewDesPath);
}
}
}
else
{
printf ("The File found is %s<BR>, FindFileData.cFileName);
char l_szSrcFile[1025] = {0};
char l_szDesFile[1025] = {0};
sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
}
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return TRUE;
}
int main(int argc, char *argv[])
{
__Copy("C:\fcdb\","E:\sandy\");
getch();
return 0;
}