Saksham
Saksham

Reputation: 9380

This program cannot be run in DOS mode

I was trying out a program and compiling and running it with GCC but it throws error that it cannot be run in dos mode. Here is my code

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char *argv[]) 
{ 
    ifstream is; 
    is.open("text1.txt",ios::binary);
    ofstream outfile;
    outfile.open("text2.txt",ios::binary);
    char ch; 
    while (is.get(ch)) 
    { 
        outfile.put(ch);
        cout << ch;  //this shows
    }
    is.close();
    outfile.close();
    getchar();
    return 0; 
}

But this code works perfectly fine in Visual Studio. Any suggestions?

Upvotes: 1

Views: 11031

Answers (2)

Pedrom
Pedrom

Reputation: 3823

If you want to do this more cross-platform friendly you could remove the line

#include<conio.h>

and change getch() for getchar()

EDIT: So it would look like this:

 #include<fstream>
 using namespace std;
 int main(int argc, char *argv[])
 {
     ifstream is;
     is.open("text1.txt",ios::binary);
     ofstream outfile;
     outfile.open("text2.txt",ios::binary);
     char ch;
     while (is.get(ch))
     {
         outfile.put(ch);
         cout << ch;  //this shows
     }
     is.close();
     outfile.close();
     getchar();
     return 0;
 } 

Upvotes: 2

lurker
lurker

Reputation: 58294

I'm supposing there's a gcc compile option for to run as a console command. See -mconsole here: http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Windows-Options.html

Upvotes: 2

Related Questions