Eclipse - how can I stop my program? - c++

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char c;
ofstream outFile("/home/gnome/Music/file.txt", ios::out);
while (cin >> c) {
outFile.put(c);
}
outFile.close();
return 0;
}
When I want to stop, I can't interrupt it with a control-D like I do on the the Linux terminal. How do I do this in eclipse. The issue is that, It doesn't write any char to my file. When I cat the file, it's empty, and the past data is gone. Empty. I used this code with g++ on the terminal, and it works.
How do I use this in eclipse?
Console

Related

Using fstream doesn't write results

I have a problem with fstream. It takes data from one file but usually it doesn't write it in a result file. I didn't have this problem before and I hadn't changed anything in settings when this started happening. Also restarting computer used to do the trick and it would start working but not anymore.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("in.txt");
ofstream out("out.txt");
int a;
in >> a;
out << a;
in.close();
out.close();
return 0;
}
if I write cout << a; it shows the number that was in data file, but with out >> it doesn't change the result file out.txt. All files are in the same folder.
If it makes a difference: I'm using codeblocks

I have a problem with reading from a file

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream file, save_file;
vector<string> words;
for (int i = 0; i < 1000; i++)
file >> words[i];
cout << words[0];
return 0;
}
I want to save these words in a vector, but I can't. I have a message: zad1.exe is already runing! Please close it first to compile successfully! I don;t know why.
Here is my solution:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::fstream file("test.txt"), save_file;
std::vector<std::string> words;
std::string line;
for (int i = 0; i < 1000; i++){
file >> line;
words.push_back(line);
}
std::cout << words[0];
return 0;
}
Instead of test.txt write your file path
On some operating systems you cannot write to a file if that file is a running program. That is what the error message is telling you. You are creating a program called zad1.exe and it is currently running so you cannot create a new version of zad1.exe until you stop the version that is already running.
Are you working on Windows? If so then use the task manager to kill any versions of zad1.exe that you can see.
Plus you have many problems with the code as pointed out in the comments above. But the first task is to kill those running programs.

Getting Information from input file C++

I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween

CodeBlocks on Mac are not reading file

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int A;
ifstream file("file.txt"); // there is a single "8" in the file
file >> A;
cout << A; // I get 0 always
return 0;
}
While I'm not new to CodeBlocks, I'm new to CodeBlocks on Mac. I have changed the "Execution working directory" and it still does not work, please help.
Don't change the execution working directory.. When you're reading from file, try writing the full directory where is that file, for example:
// this is your file.txt location
ifstream file("C:\\Desktop\\file.txt"); // this is for Windows
and then run a program.
If it still doesn't work, try watching this tutorial: https://www.youtube.com/watch?v=De6trY8FRYY

How can I do file i/o without fstream for competitions like google code jam?

Let me preface this question by saying I am not a very experienced programmer.
For competitions like google code jam, I write code like this:
#include <fstream>
using namespace std;
int main() {
ifstream fin("file.in");
ofstream oin("file.out");
//Etc. I'll now write out my solution.
//...
}
However, I noticed that many other code sources by other participants don't use fstream at all and use iostream instead. Then they'll use cout and cin as if they were reading and writing from the console.
How are they doing this? Can I do the same thing if I am using g++ and ubuntu?
EDIT: Since it was requested that I post an example of what I mean, here is code from participant ryuuga who solved large Bot Trust, Problem A from the recent '11 qualification round.
He uses cin and cout but I don't know how he is doing file i/o.
#include <iostream>
using namespace std;
#include <cstdio>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
typedef pair<int,int> pii;
#include <vector>
typedef vector<int> vi;
#include <queue>
#include <stack>
#define For(i,a,b) for(int i=(a);i<(b);++i)
#define ForI(i,a,b) for(int i=(a);i<=(b);++i)
#define ForAll(it,set) for(typeof(set.begin()) it = set.begin(); it!=set.end(); ++it)
typedef stack<int> si;
typedef queue<int> qi;
int main(){
int t;
cin>>t;
ForI(tt,1,t){
int n;cin>>n;
int pos[2]={1,1}, time[2] = {0,0};
int curTime = 0;
For(i,0,n){
char type; int button;
cin>>type>>button;
type = (type=='B'?1:0);
int nextTime = 1+max(curTime, time[type] + abs(button - pos[type]));
pos[type] = button;
time[type] = nextTime;
curTime = nextTime;
}
cout<<"Case #"<<tt<<": "<<curTime<<endl;
}
return 0;
}
Imagine when you are using the shell in ubuntu. Almost everything is read by the console, cin and written to the console, cout. For instance
cat "file.txt" | grep "Hello"
cat would get the "file.txt" from the arguments given to main, i.e.
main(int argc, char ** argv){
// for the both cat and grep examples argc is 2
// argv[1] contains "file.txt" for cat.
// open an ifstream and ouput it to cout. There's cat for you.
}
To find out what argv[0] contains, try it out!
grep would read the same argument then read everything from cin, copy the input that matches argv[1] to cout
EDIT: He is running his program with
cat "downloaded-input-file.txt" | theprogram > output.txt
Then submits the program. Or he might be using the < downloaded-input-file syntax. I'm keeping the initial explanation as it might be useful for the understanding of the process.