Getting a build error in c++ - c++

I am getting a weird C3681 error, keeps saying that an identifier cannot be found for one of my functions. I am very confused and have tried solutions found while using Google, but I cannot get it resolved. I would prefer not to use stl.
The error:
Error 1 error C3861: 'readTheStuff': identifier not found c:\users\xxxxxx\desktop\data structures\homework2\homework2\editor.cpp 38 1 Homework2
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int readFile(const char *fileName) {
ifstream myReadFile;
string line;
int i = 0;
myReadFile.open(fileName);
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
getline(myReadFile, line);
line += " "; //adds a space after every line
//cout << line << endl;
readTheStuff(line);
}
}
myReadFile.close();
return 0;
}
void readTheStuff(string command){
cout << command; //testing
}
int main(int argc, const char* argv[]){ //when they call, going to pass two parameters
if (argc > 2){
cout << "Error, more than one file given" << endl;
}
else if (argc < 2){
cout << "Error, no file given" << endl;
}
else if (argc == 2){
readFile(argv[1]);
}
}

You have to declare a function before you can call it. Either make a declaration of readTheStuff before readFile or, more simply, just move the whole function up above readFile

2 ways to fix this.
1.) Include the following line just underneath your #includes in editor.cpp
void readTheStuff(std::string command)
or include the following line in addition to your other #includes
#include "editor.h"
inside editor.h the following line would need to appear
void readTheStuff(string command)
This is called prototyping and allows the compiler to link readTheStuff(line) to the actual function reaTheStuff(string command).

Related

Trying to make something write to a file in c++

I tried programming a file writer, but when i try to write to a file with something that has multiple words it will suddenly create files.
My code
#include <fstream>
#include <iostream>
#include <unistd.h>
int main(int argc, char *argv[]) {
char cwd[256];
while (true) {
getcwd(cwd, 256);
std::string cwd_s = (std::string)cwd;
std::string Input;
std::cout << cwd_s << "> ";
std::cin >> Input;
std::ofstream file(Input);
std::cout << "cmd /";
std::cin >> Input;
file << Input;
};
for (int i; i < argc; i++) {
std::cout << argv[i] << '\n';
};
return 0;
}
I expected to get this:
C:\Users\code> File.txt
cmd /hello world!
File.txt
hello world!
But it only had "hello", it created another file named world!
I have tried changing the code, but to no avail.
So I have wrote this code that I think does what you expect. The behavior you were seing is because you used the same string to store the filename and the user input. Also you redefined a new file every loop (without closing the previous one). I added a signal handler since if you press Ctrl+C the program would quit without saving/closing the file.
I added comments about how you can make a better CLI interface (if you're interested)
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
std::ofstream outfile;
void signalHandler(int signum) {
outfile.close();
exit(signum);
}
int main() {
char cwd[256];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
std::cout << cwd << "> ";
} else {
std::cerr << "Error: Could not get current working directory." << std::endl;
return 1;
}
std::string filename;
std::getline(std::cin, filename);
outfile.open(filename);
// We intercept the Ctrl+C signal to close the file before exiting. Else nothing will be written to it.
// You can also use Ctrl+D (EOF: End Of File) to exit the program.
// The best praticte would be to implement a command line interface with a "quit" command. (like a map<string, function> for example)
signal(SIGINT, signalHandler);
// Another good practice is to check if the file did open correctly.
if (!outfile.is_open()) {
std::cerr << "Error: Could not open file for writing." << std::endl;
return 1;
}
std::cout << "cmd / ";
char ch;
while (std::cin.get(ch)) {
outfile.put(ch);
if (ch == '\n') {
std::cout << "cmd / ";
}
}
outfile.close();
return 0;
}
Hope it will help you ! And if you have any question about the code feel free to ask I'll explain !

How do you open a file using a string from an array?

I have an array of file names that I need to open. When I put in the plans.open. It gives me the error "no matching function for call to 'std::basic_ifstream::open(std::__cxx11:..."
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int main(){
ifstream files;
ifstream plans;
string stufiles[100];
int numFiles,timeBlocks;
files.open("filesToProcess.txt");
if (files.fail()){ //checks to see if the selected store file opened
cout << "Error when opening file!" << endl;
return 0;
}
files >> numFiles;
for (int i= 0; i<= numFiles; i++) {
files >> stufiles[i];
}
files.close();
cout << stufiles[0] << endl;
plans.open(stufiles[0]);
if (plans.fail()){ //checks to see if the selected store file opened
cout << "Error when opening file!" << endl;
return 0;
}
}
This is supposed to open the file using the file name in the array.
It gives me the error "no matching function for call to 'std::basic_ifstream::open(std::__cxx11:..."
Your compiler's version of std::ifstream::open() does not support std::string as input, so you will have to give it a const char* instead. You can use std::string::c_str() for that:
plans.open(stufiles[0].c_str());

Passing a file into a function

I'm trying to create a program that passes a file to a function. The function is supposed to detect how many lines are in my file. I don't think I'm passing the file correctly into my function, I've tried several different ways. Any help will be greatly appreciated.
#include <iostream>
#include <fstream>
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
int num_of_lines(ifstream file)
{
int cnt3;
string str;
while(getline(file, str))cnt3++;
return(cnt3);
}
int main(int argc, char **argv)
{
int num_of_lines(ifstream file);
string file;
file = argv[1];
if(argc == 1)die("usage: mywc your_file"); //for some reason not working
ifstream ifs;
ifs.open(file);
if(ifs.is_open())
{
int a;
cout << "File was opened\n";
a = num_of_lines(file);
cout <<"Lines: " << a << endl;
}
else
{
cerr <<"Could not open: " << file << endl;
exit(1);
}
ifs.close();
return(0);
}
Two problems with the function. First, you should pass the stream by reference. Second, you just forgot to initialise your counter.
int num_of_lines( ifstream &file )
{
int cnt3 = 0;
string str;
while( getline(file, str) ) cnt3++;
return cnt3;
}
The other thing is you're passing file to it (which is a string) instead of ifs. Change the call to:
a = num_of_lines( ifs );

Multiple definition error on the same line. (C++)

I have a new complicated problem. The compiler complains that I am redefining a function, but it says that the first place I declared it at has the site of re-declaration. The problem began as soon as I included the cpp file in another. In attempt to fix my problem I exported it to a hpp file, but to know avail. Here is my code.
main.cpp:
#include <iostream>
#include <string>
#include "main.hpp"
using namespace std;
int main(int argc, char *argv[])
{
//Deal with arguments and send them to the correct functions
if (argc >= 2){
string op = argv[1];
if (op == "-a" || op == "--automatic"){
if (argc >= 3){
string FName = argv[2];
bool dbgbool;
if (argc == 4){
string dbgstring = argv[3];
if (dbgstring == "debug"){
dbgbool = true;
}
}
Lexer(FName, dbgbool);
}
}
else{
cout << "Invalid Argument\n";
goto help;
}
return 0;
}
//Or, just write help and info
help:
cout << "\n";
cout << "bwc v0.0.1U-(Unstable)\n\n";
cout << "Usage: bwc <operation> [...]\n";
cout << "Operations:\n";
cout << " bwc {-a --automatic} <file(s)>\n";
cout << " bwc {-i --interactive}\n";
cout << " bwc {-c --error-codes}\n";
cout << "\n";
return 0;
}
LA.cpp:
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;
string Lexer(string FileN, bool dbg){ //This is the line of re-declaration.
//If debugging,this writes out put to the console
if (dbg == true)
cout << "Beginning Lexical Analysis...\n";
//Create new file stream and set it equal to the source file
ifstream Ifile (FileN.c_str());
//Test if the last step failed, if so, write an error to the console, and terminate the compiler
if (!Ifile.is_open()){
cout << "Unable to open file. Path to file may not exist, or the file name could be incorrect.\n";
cout << "Error Code: -1\n";
return NULL;}
//Create new stringstream, and set it equal to the source file
string IFStream;
Ifile >> IFStream;
//Close the source file
Ifile.close();
//If debugging,this writes out put to the console
if (dbg == true)
cout << "Source file sucessfully read.\n";
//Set out stream equal to the modified in stream
string OFStream = IFStream;
return OFStream;
}
and finally,
main.hpp:
#ifndef MAIN_HPP_INCLUDED
#define MAIN_HPP_INCLUDED
#include "LA.cpp"
extern string Lexer(string,bool);
#endif // MAIN_HPP_INCLUDED
Thanks,
Brooks Rady
Your main.cpp is including main.hpp which is including LA.cpp, so the contents of LA.cpp are being compile once for LA.cpp and once for main.cpp.
.hpp files should contain only declarations (string Lexer(string,bool);), while the definitions (string Lexer(string,bool) {... }) should go in the .cpp
You will not see this kind of issue when you are dealing with class methods, because the compiler accepts definitions of methods. But functions should be defined only in the .cpp files.

ifstream not working

I'm trying to open a file using ifstream, but no matter what solutions I find that I've tried, nothing seems to work; my program always outputs "unable to open". Below is my code in its entirety. Any help at all is appreciated!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char ** argv)
{
string junk;
ifstream fin;
fin.open("somefile.txt");
if(fin.is_open())
{
fin >> junk;
cout << junk;
}
else
{
cout << "unable to open" << endl;
}
fin.close();
return 0;
}
Also, the contents of somefile.txt, which is in the same directory as the created executable is the following:
SOME
FILE
As some commenters have suggested, it could easily be that the file truly doesn't exist, because you're looking for it in the wrong place. Try using an absolute path to the file rather than just assuming it's looking where you expect.
And output a more helpful error message using strerror(errno).
// ...
fin.open("C:\\path\\to\\somefile.txt");
// ...
else
{
cout << "unable to open: " << strerror(errno) << endl;
}