Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to write a simple backup program. Its not finished yet, but i've encountered a problem: My class responsible for setting the right path wont execute the worker which will copy the file. I don't know why and yes - i already looked up on any helping site i know. Here is my filecopy h code:
#ifndef __FILECOPY_H_INCLUDED__
#define __FILECOPY_H_INCLUDED__
#include<iostream>
#include<fstream>
#include<ctime>
class filecopy
{
std::string dest_path;
std::string src_path;
public:
filecopy(std::string, std::string);
void filecopy_worker()
{
std::cout << "FILECOPY PROCESS STARTED" << std::endl;
std::ifstream source(src_path);
std::ofstream dest(dest_path);
dest << source.rdbuf();
source.close();
dest.close();
}
};
filecopy::filecopy(std::string a, std::string b)
{
dest_path = a;
src_path = b;
}
#endif
And here my main.cpp code:
#include<iostream>
#include<stdlib.h>
#include"filecopy.h"
int main(int argc, char *argv[])
{
if(argc != 3)
{
std::cout << "USAGE: " << argv[0] << " <filesource>" << std::endl;
return 1;
}
else
{
filecopy target1(argv[2], argv[1]);
std::cout << "TARGET ASSIGNED" << std::endl;
std::cout << "EXECUTE FILEWORKER" << std::endl;
}
return 0;
}
It didn't execute the function because you never called it. Just add that function call
filecopy target1(argv[2], argv[1]);
target1.filecopy_worker();
Related
This question already has an answer here:
c++ - Doesn't name a type
(1 answer)
Closed 6 months ago.
I edit the code to clarify the actual code :
#include <fstream>
#include <iostream>
#include <ros/ros.h>
#include <rosbag/bag.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>
#include <nav_msgs/Odometry.h>
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
runtimeFile.open();
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
runtimeFile.close ();
std::cout << "Runtime data stored." << std::endl;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "main");
ros::NodeHandle nh;
ros::Subscriber Listener = nh.subscribe<nav_msgs::Odometry>("/odom", 100, callhandler);
ros::spin();
return 0;
}
error: `‘runtimeFile’ does not name a type
9 | runtimeFile.open ("cmg_operations_runtime.txt")
The error is the same, I hope someone to help me in this issue?`
In C++ all code must be inside a function. Additionally all C++ programs must have a function called main.
Further your code opens the file twice, once when you declare the runtimeFile variable and once when you call open. Did you not think it strange that you have the file name twice in your code? Don't open files twice. Finally, although it's not an error, there is no need to close the file, that will happen automatically.
Put all that together and you have a legal C++ program.
#include <fstream>
int main()
{
std::fstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
runtimeFile << "tempVector[j]" << ";\t";
}
EDIT
Some real code has been posted. Based on that I would remove the global runtimeFile variable and make it local to callHandler like the following
void callhandler(const nav_msgs::Odometry::ConstPtr& msg)
{
std::ofstream runtimeFile("cmg_operations_runtime.txt" , std::ios::out);
if (!runtimeFile)
{
std::cout << "cmg_operations_runtime.txt could not be opened.";
}
runtimeFile << "tempVector[j]" << ";\t";
std::cout << "Runtime data stored." << std::endl;
}
However I can't really see how the latest posted code causes the error described.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to C++ and visual studio, can anyone please help me in creating an UI for below requirements?
Requirement: There is a .txt file which consists of 10 test cases. In the UI, I(tester) should be able to select his/her choice of test cases to run.
Try using a MFC project. You can find drop-down menus in GUI toolbox.
Learn further in MFC.
refer this - https://www.youtube.com/watch?v=6hSYZdvQ3s4&index=1&list=WL
Consider the possibility to use command line interface as the following template shows:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
// check that parameter is given in command line when program is run
if(argc != 2) // if number of parameters is insufficient
{ // explaine and stop
cout << "Please, run a program with parameter: name of file with testcases" << endl;
cout << "For example:" << endl << " " << argv[0] << " tests.txt" << endl;
return 1;
}
// try to open file with test cases
ifstream testfile;
testfile.open(argv[1], ifstream::in);
if( !testfile.is_open() )
{
cout << "ERROR: File " << argv[1] << " cannot be open!" << endl;
return 2;
}
// read test cases from file
string testCaseName;
char answer;
while( getline(testfile, testCaseName).good() )
{
if(testfile.eof())
{
break;
}
if(testCaseName.length() == 0)
{
continue;
}
cout << "Would you like to execute testcase " << testCaseName << " ? (Y/N): ";
cin >> answer;
if( answer == 'Y' || answer == 'y')
{
cout << "Test execution... " << endl;
// run test case
// . . .
// report test results
// . . .
}
}
return 0;
}
To run your test with such program you should type in the command line:
programname test_file.txt
This question already has an answer here:
gnuplot-cpp cannot feed command to pipe
(1 answer)
Closed 8 years ago.
I'm trying to implement the code below from this site:
https://sites.google.com/site/bettereaclone/introduction/gnuplot/c-example-gnuplot-1
Code:
#include <iostream>
#include "gnuplot_i.hpp"
#include <windows.h>
#include <conio.h>
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
try
{
Gnuplot g1("lines");
cout << "*** plotting slopes" << endl;
g1.set_title("Slopes\\nNew Line");
cout << "y = x" << endl;
g1.plot_slope(1.0,0.0,"y=x");
cout << "y = 2*x" << endl;
g1.plot_slope(2.0,0.0,"y=2x");
cout << "y = -x" << endl;
g1.plot_slope(-1.0,0.0,"y=-x");
g1.unset_title();
}
catch (GnuplotException ge)
{
cout << ge.what() << endl;
}
return 0;
}
I installed the gnuplot (http://www.gnuplot.info/download.html)
gnuplot_i.hpp file (https://code.google.com/p/gnuplot-cpp/source/browse/trunk/gnuplot_i.hpp)
When I run this code, I get this problem:
The error:
'C:/Program' is not recognized as an internal or external command,
operable program or batch file.
I've no idea why. thanks!!
Looks like path to gnuplot is malformed, not in quotation marks or simply not read properly.
Try forcing the path by uncommenting this line and correcting it to point gnuplot directory on your machine:
// Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
This question already has answers here:
launch an exe/process with stdin stdout and stderr?
(3 answers)
Closed 8 years ago.
I need some help on this subject.
I have a C++ .exe that I want to open with C++ and then write some arguments in the console.
Here is an example of what I want to do:
Lets assume an executable whatsyourage.exe with this code (in reallity, I don't have the corresponding code):
#include <iostream>
using namespace std;
int main()
{
int age = 0;
cout << "What's your age ?" << endl;
cin >> age;
cout << "You are " << age << " year !" << endl;
return 0;
}
and I want to do something like:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";// in the same library
std::string const command = chemin+" "+ invit;
system(command.c_str());
}
I want to write the age (21).
Can someone please help me?
Here is the answer:
int main()
{std::string invit="21";
std::string chemin ="whatsyourage.exe";
FILE* pipe = _popen(chemin.c_str(), "w");
if (pipe == NULL) {
perror("popen");
exit(1);
}
fputs("30", pipe);// write the age into the pipeline
pclose(pipe); // close the pipe
}
The popen() function from POSIX does what you are looking for. It allows you to execute a program (like system) while getting a file handle on its input/output streams.
For Windows, if popen() is not available, you can use the CreatePipe() & co functions to do the same thing; check out this question for some pointers.
The second snippet you added is good, and the problem is with the first code. In order to handle the command line from a program, you have to define the main as int main(int numberOfArguments, char* arguments[]) (often people use the shorter version - main(int argc, char* argv[]), but you can name the arguments as you wish). Then, if you pass an argument to the function, it will be in argv[1], since argv[0] is always the path to the executable. So the first program should look like this:
int main(int numberOfArguments, char* arguments[])
{
if(numberOfArguments>=2)
cout << "You are " << arguments[1] << " year !" << endl;
else
cout << "Too few arguments passed!" << endl;
return 0;
}
I'm doing a C++ assingment for a class and I haven't used C++ in a decade so this might be something I'm missing that is simple; however ,I can't seem to figure it out.
I have a class I defined with a function that is producing no output; it looks like it's not even running and I don't have a clue why. Could someone point out my problem to me?
Issue: cout from the function getwords of the class readwords doesn't display any results.
Here is my class:
class readwords {
private:
char c;
//string aword;
public:
void getwords(std::istream& file) {
cout << "I got here" << std::flush;
/*while(file.good()) {
cout << "I got here\n";
c = file.get();
if(isspace(c)) cout << "\n"; //continue;
if(isalnum(c)) {
cout << c; //aword.insert(aword.end(),c);
}
}
*/
}
};
Which is being called from my main:
#include <fstream>
#include <stdlib.h>
#include "lab1.h"
using namespace std;
readwords wordsinfile;
words wordslist;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) {
// Looks like we have no arguments and need do something about it
// Lets tell the user
cout << "Usage: " << argv[0] <<" <filename>\n";
} else {
// Yeah we have arguements so lets make sure the file exists and it is readable
ifstream ourfile(argv[1]);
if (!ourfile.is_open()) {
// Then we have a problem opening the file
// Lets tell the user and exit
cout << "Error: " << argv[0] << " could not open the file. Exiting\n";
exit (1);
}
// Do we have a ASCII file?
if (isasciifile(ourfile)) {
cout << "Error: " << argv[0] << " only can handle ASCII or non empty files. Exiting\n";
exit(1);
}
// Let ensure we are at the start of the file
ourfile.seekg (0, ios::beg);
// Now lets close it up
ourfile.close();
}
// Ok looks like we have past our tests
// Time to go to work on the file
ifstream ourfile2(argv[1]);
wordsinfile.getwords(ourfile2);
}
Thank you for any help you can provide.
Try to use a debugger. Most IDEs (NetBeans, Code::Blocks, etc) provide an interactive interface with gdb.
I just compiled and ran your code, but nothing wrong with the code itself,
except that I needed to include to use the 'cout' method.
"I got here" has been successfully displayed in my ubuntu machine.
What is your execution environment? You should check it first.
The problem appears to be redefining my own class. When actually coding the function I needed to use:
in readwords::countwords(std::istream& file) {
....
}
Once doing this output produced fine.