"'main': identifier not found" error in c++ - c++

I keep getting this error and have no idea how to fix it because I don't see anything wrong with my code.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}

The main function is not intended to be invoked from your code. It is also a nonsense since it is called automatically when program starts. But if you need some alternative main to be invoked upon some condition, you can call it inside your main function
...
int alternative_main()
{
place your program there
}
...
void file_create(std::string name) {
...
if (file.is_open()) {
...
alternative_main()
}
else {
...
}
}
Suppose your main looks like this
int main() {
file_create("myfile");
}

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
int main(); // defined before file_create
void file_create(std::string name) {
std::ifstream file(name);
if (file.is_open()) {
file.close();
std::cout << "File already exists..." << std::endl;
main();
}
else {
file.close(); std::ofstream newFile(name);
if (newFile.is_open())
std::cout << GREEN "New file successfully created..." << RESET << std::endl;
else
std::cout << RED "File could not be created" << RESET << std::endl;
newFile.close();
}
}
int main() {
}
Define int main() above file_create() so you can call it in file_create

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
//use this instead of repetitively using std all the time
#define GREEN "\033[32m"
#define RED "\033[31m"
#define RESET "\033[0m"
void file_create(string name) {
ifstream file(name);
if (file.is_open()) {
file.close();
cout << "File already exists..." << endl;
return 0;
}
else {
file.close();
ofstream newFile(name);
if (newFile.is_open())
cout << GREEN "New file successfully created..." << RESET << endl;
else
cout << RED "File could not be created" << RESET << endl;
newFile.close();
}
}
int main() {
string name;
cin>>name;
file_create(name);
}
try using this way...

Related

Creating new txt files with c++ program, naming with variables

I have created a program and I want to create with it files like aff1.txt, aff2.txt, etc. In these files, I want to have here a text created this way: It will open the file: text.txt and it will take each sentence, copy it 4700/sentence length times to each file. But it isn't working, when: cout << ss << endl;, it writes to cmd nothing, while there should be something, which was assigned before. What should I do?
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int main()
{
ifstream vstup("text.txt"); // 4700,2700,2200,1700
string vety;
getline(vstup,vety);
vstup.close();
string ss="affn.txt";
char q[vety.length()];
for (int u=0;u<vety.length();u++)
{
q[u] = vety[u];
}
int l=0,m=0,n=0;
int v,i,e,o;
char vl[999999];
//cout << vety.length() << endl;
for (i=0;i<vety.length();i++)
{
//cout << "ss" << endl;
if (q[i]=='.')
{
// cout << "ss" << endl;
v=4700/i;
for (e=0;e<v;e++)
{
//cout << "ss" << endl;
for (o=0;o<i-l;o++)
{
// cout << "ss" << endl;
m=o+e*(i-l);
vl[m]=q[o+l];
}
}
l=l+i;
cout << vl << endl;
n++;
//ofstream aff("aff.txt");
//aff << vl << endl;
//aff.close();
ss[3]=n;
ofstream writer(ss.c_str());
//writer.open(ss.c_str());
writer << vl << endl;
writer.close();
cout << ss << endl;
ss.clear();
}
}
return 0;
}

C++ function unable to another function and ends automaticly

So I was making a file editor using c++ and it has 3 functions and it needs to call each other to work properly.But When code tries to call other functions it end abnormly .
I tried changing the order of functions but it does nothing.It will compile properly without warnings
it needs output the contents of the file.
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */
/* data */
char buffer;
std::string fname;
int reader(){
std::ifstream readfile;
readfile.open(fname.c_str());
readfile>>buffer;
std::cout << buffer<< '\n';
int write();
}
int options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >>fname;
cout << "Opening File"<<fname<< '\n';
int reader();
std::cout << buffer<< '\n';
}
int write(){
cout << "writing to file " << '\n';
std::ofstream writefile;
writefile.open(fname.c_str());
writefile<<buffer;
cout << "writing done " << '\n';
}
int main()
{
/* code */
options();
return 0;
}
options() is not calling reader(), and reader() is not calling write(). In both cases, you are simply declaring functions, not actually calling them.
int reader(){
...
int write(); // <-- a declaration, not a call!
}
int options(){
...
int reader(); // <-- a declaration, not a call!
...
}
int main() {
...
options(); // <-- a call, not a declaration!
..
}
Try this instead:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/* data */
char buffer;
std::string fname;
int reader(){
cout << "opening file " << fname << '\n';
std::ifstream readfile(fname.c_str());
readfile >> buffer;
std::cout << buffer << '\n';
}
int write(){
cout << "writing to file " << '\n';
std::ofstream writefile(fname.c_str());
writefile << buffer;
cout << "writing done" << '\n';
}
int options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >> fname;
reader();
write();
}
int main() {
/* code */
options();
return 0;
}
In addition to the comments above about calling the functions, it seems like it would be good to initialize buffer as a char array as shown below:
#include <iostream>
#include <fstream>
//#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <unistd.h>
#include <sstream>
using namespace std;/* std */
/* data */
char buffer[]{"Short test"};
std::string fname;
void write(){
cout << "writing to file " << '\n';
std::ofstream writefile;
writefile.open(fname.c_str());
writefile<<buffer;
cout << "writing done " << '\n';
}
void reader(){
std::ifstream readfile;
readfile.open(fname.c_str());
readfile>>buffer;
std::cout << buffer<< '\n';
write();
}
void options(){
cout << "************************"<< '\n';
cout << "* Starting File editor *"<< '\n';
cout << "************************"<< '\n';
cout << "* Enter Filename *"<< '\n';
cin >>fname;
cout << "Opening File"<<fname<< '\n';
reader();
std::cout << buffer<< '\n';
}
int main()
{
/* code */
options();
return 0;
}
You can declare functions(not compulsory in your case) after all #include statements like:
int reader();
int write();
int options();
You call write function as write(); reader function as reader();
Since functions are not returning anything you could change int reader() to void reader(), int write() to void write() and so on. Keep main as int main() though.

C++: Text file not being read

I'm trying to print string values from a text file to the console but the file isn't opening.
#include "stdafx.h"
#include <iostream>
#include <string>
#include<fstream>
int main()
{
ifstream nameFile;
nameFile.open("E:\\Names.txt");
string a;
int studentCount = 0;
if (nameFile.is_open())
{
while (getline(nameFile, a)) {
cout << a << '\n';
studentCount++;
}
cout << studentCount;
}
else
cerr << "Unable to load file" << endl;}

Visual Studio 2015 C2011 'Class' type redefinition

im currently working on a small console app, i read from some files, and i have made a simple animated loading bar thread to show the progress of file reading (sometimes the file are really huge)
i have read on the microsoft documentation and on the forum and that error seems to suggest that i defined the class multiple time.
but i did include all the header blocks to prevent this from happenning, any of you see my mistake, probly obvious, i havent done c++ in years.
here is the code
fileReader.h
#pragma once
#ifndef FILEREADER_H // must be unique name in the project
#define FILEREADER_H
#include <vector>
using namespace std;
class fileReader {
private:
public:
vector<string> readTextFile(string path);
unsigned int getSize();
unsigned int getLoadedBytes();
};
#endif // FILEREADER_H
#pragma once
fileReader.cpp
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
#include "numbers.h"
#include "loadingBar.h"
using namespace std;
class fileReader {
private:
public :
vector<string> list;
int i;
unsigned int size;
unsigned int loadedBytes;
string line;
std::thread* barThread;
vector<string> fileReader::readTextFile(string path) {
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
i = 0;
size = 0;
loadedBytes = 0;
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
cout << "\t" << path << " (" << formatNumbers(size) << " bytes)" << endl;
file.clear();
file.seekg(0, ios::beg);
barThread = new std::thread(&loadingBar::run, *this);
while (file >> line) {
list.push_back(line);
loadedBytes += strlen(line.c_str());
i++;
}
}
else {
cout << "Error reading : \"" << path << "\"" << endl;
exit(EXIT_FAILURE);
}
file.close();
return list;
}
unsigned int fileReader::getSize() { return size; }
unsigned int fileReader::getLoadedBytes() { return loadedBytes; }
};
loadingBar.h
#pragma once
#ifndef LOADINGBAR_H
#define LOADINGBAR_H
#include<stdlib.h>
#include<string>
#include<iomanip>
#include "numbers.h"
#include "fileReader.h"
using namespace std;
class loadingBar {
public:
void notififyFinish();
void notifyError();
void notifiError(string);
void drawloadingBar();
void drawPathBar();
void drawBytesBar();
void setLoadedUnits(int);
void setTotalUnits(int);
void run(fileReader*);
};
#endif // NUMBERS_H
loadingBar.cpp
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include "numbers.h"
#include "fileReader.h"
using namespace std;
class loadingBar {
private:
public:
double totalUnits = 1;
double loadedUnits = 0;
char loadedChar = '/';
char emptyChar = ' ';
int barLength = 50;
double percent;
int nbChar;
void notiftyFinish() {
cout << " Ok" << endl;
//exit(EXIT_SUCCESS);
}
void notifiyError() {
cout << " Error !" << endl;
//exit(EXIT_FAILURE);
}
void notifiyError(string errMess) {
cout << " Error !" << endl << "\t" << errMess;
//exit(EXIT_FAILURE);
}
void drawLoadingBar() {
cout << fixed;
cout << "\rLoading [";
for (int i = 0; i < nbChar; i++)
cout << loadedChar;
for (int i = nbChar; i < barLength - 1; i++)
cout << emptyChar;
cout << "] " << setprecision(0) << percent << "%";
}
void drawPathBar(string path) {
cout << fixed;
cout << "\rLoading [ ";
cout << path;
cout << " ] " << setprecision(0) << percent << "%";
}
void drawBytesBar() {
cout << fixed;
cout << "\rLoading [ ";
cout << formatNumbers(loadedUnits) << " / " << formatNumbers(totalUnits);
cout << " ] " << setprecision(0) << percent << "%";
}
void setLoadedUnits(int newValue) {
if (newValue > 0)
loadedUnits = newValue;
}
void setTotalUnits(int value) {
if (value > 0)
totalUnits = value;
}
void run(fileReader *f) {
setTotalUnits(f->getSize());
setLoadedUnits(f->getLoadedBytes());
while (loadedUnits <= totalUnits) {
setLoadedUnits(f->getLoadedBytes());
percent = ((double)(loadedUnits / totalUnits) * 100);
nbChar = (int)(percent / (int)(100 / barLength));
drawLoadingBar();
//setLoadedUnits((int)loadedUnits + 10);
if (loadedUnits >= totalUnits) notiftyFinish();
}
}
};
Your .cpp files redefine your classes. You've already defined them in the respective .h files. All you need to include in your .cpp files is the implementations. They should look more like this:
fileReader.cpp
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <thread>
#include "numbers.h"
#include "loadingBar.h"
vector<string> fileReader::readTextFile(string path) {
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
i = 0;
size = 0;
loadedBytes = 0;
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
if (file.is_open()) {
size = file.tellg();
cout << "\t" << path << " (" << formatNumbers(size) << " bytes)" << endl;
file.clear();
file.seekg(0, ios::beg);
barThread = new std::thread(&loadingBar::run, *this);
while (file >> line) {
list.push_back(line);
loadedBytes += strlen(line.c_str());
i++;
}
}
else {
cout << "Error reading : \"" << path << "\"" << endl;
exit(EXIT_FAILURE);
}
file.close();
return list;
}
unsigned int fileReader::getSize() { return size; }
unsigned int fileReader::getLoadedBytes() { return loadedBytes; }
Similarly for loadingBar.cpp.

write Unicode strings into a txt file

I made my simple txt scanner who writes the text into a file that matches my selection. The problem is writing to file when instead of the pen writes, for example, 洀漀. On picture you can see for example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int offset;
wstring DBSearchLine, ScanLine;
wifstream ScanFile, DBSearchFile;
wofstream ResultFile;
ScanFile.open("ScanFile.txt", ios_base::binary);
ResultFile.open("ResultFile.txt", ios::out, ios_base::binary);
if (ScanFile.is_open())
{
while (!ScanFile.eof())
{
DBSearchFile.open("DBSearchFile.txt", ios_base::binary);
if (!DBSearchFile.is_open())
{
cout << "Error open DBSearchFile.txt" << "\n";
break;
}
getline(ScanFile, ScanLine);
wcout << "Scan line is - " << ScanLine << "\n";
while (!DBSearchFile.eof())
{
getline(DBSearchFile, DBSearchLine);
wcout << "DBSearchLine is -" << DBSearchLine << "\n";
if ((offset = ScanLine.find(DBSearchLine, 0)) != string::npos)
{
ResultFile << ScanLine << L"\n";
}
}
DBSearchFile.close();
}
ScanFile.close();
}
else
{
cout << "Error open ScanFile.txt" << "\n";
}
system("PAUSE");
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <codecvt>
using namespace std;
int main()
{
/* via http://stackoverflow.com/a/5105192/4005233
changes the encoding of the console and all subsequently opened
files */
std::locale::global(std::locale(""));
wifstream ScanFile;
ScanFile.open("ScanFile.txt", ios_base::binary);
if (!ScanFile.is_open()) {
cout << "Error open ScanFile.txt" << "\n";
return 1;
}
wofstream ResultFile("ResultFile.txt", ios::out);
while (!ScanFile.eof())
{
wifstream DBSearchFile;
DBSearchFile.open("DBSearchFile.txt", ios_base::binary);
if (!DBSearchFile.is_open())
{
cout << "Error open DBSearchFile.txt" << "\n";
break;
}
wstring ScanLine;
getline(ScanFile, ScanLine);
wcout << "Scan line is - " << ScanLine << "\n";
do
{
wstring DBSearchLine;
getline(DBSearchFile, DBSearchLine);
// have all lines been read?
if(!DBSearchLine.length())
break;
wcout << "DBSearchLine is -" << DBSearchLine << "\n";
if (ScanLine.find(DBSearchLine, 0) != string::npos)
{
ResultFile << ScanLine << L"\n";
break; // found a match, no need to search further
}
}while(1);
DBSearchFile.close();
}
ScanFile.close();
return 0;
}
This was tested using files with and without a BOM.
The innermost loop had to be changed to handle files with a newline character at the end; if I hadn't done that it would have match with an empty string which is always true.
(I've also changed a few other things according to my coding style, the important change is the one right at the top)