Wrong file output...
Then i run my program i get one last word out of all input words.
But I need to get all with newlines. Help please.
For ex.
OUTPUT:
MOV PRINT
ADD BRACKET
hello
ADD BRACKET |
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string input;
string type;
string output;
int number = 0;
void scan () {
if (input == "print") type = "func";
else if (input == "*") type = "brac";
}
void codegen () {
if (input == "print") output = "MOV PRINT";
else if (input == "*") output = "ADD BRACKET";
else output = input;
}
void inpuf () {
ifstream myfile2 ("program.txt");
if (myfile2.is_open())
{
while ( myfile2.good() )
{
getline (myfile2,input);
}
myfile2.close();
}
else cout << "Unable to open file";
}
int main(int argc, char *argv[])
{
inpuf ();
codegen ();
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << output;
myfile.close();
}
else cout << "Unable to open file";
cin.get ();
return 0;
}
|
The problem is of course on line 31. You keep overwriting the input. Change it to:
1 2 3 4 5 6
|
string line;
while ( getline (myfile2,line) )
{
input += line;
// ? input += "\n";
}
|
Topic archived. No new replies allowed.