Computing.Net > Forums > Programming > C++ code probs

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ code probs

Reply to Message Icon

Name: djas
Date: February 2, 2005 at 15:20:33 Pacific
OS: Windows XP Pro SP2
CPU/Ram: P4 2.40 GHZ 256 RAM
Comment:

Is anyone able to tell me whats wrong with this code, im a noob at this so any help is welcome.

-=CODE=-
#include <iostream>
using namespace std;

int main();
{
ifstream myFile("server.txt");
if (! myFile)
{
cout << "Error opening input file" << endl;
return -1;
}
while (! myFile.eof())
{
myFile.get(ch);
ofstream myfile("server.exe");
}
if (! myfile)
{
cout << "Error opening output file" << endl;
return -1;
}
myFile << ch << endl;
myFile.close();

return 0;
}
-=CODE=-

thanx in advance


djas



Sponsored Link
Ads by Google

Response Number 1
Name: egkenny
Date: February 2, 2005 at 17:30:33 Pacific
Reply:

I am not sure what you are trying to do but this code works:

#include <iostream>
#include <fstream> // required for ifstream and ofstream
using namespace std;

int main()
{
char ch; // input character
ifstream myFile("server.txt");
if (myFile.fail()) // check ios::fail flag
{
cout << "Error opening input file" << endl;
return -1;
}
ofstream myfile("server.exe");
if (myfile.fail()) // check ios::fail flag
{
cout << "Error opening output file" << endl;
return -1;
}

// must at least read one character to test for
// empty file if empty file then eof() will be
// true and while loop will be bipassed
myFile.get(ch);
while (!myFile.eof())
{
myfile << ch << endl;
myFile.get(ch);
}
myFile.close();
myfile.close();

return 0;
}


0

Response Number 2
Name: djas
Date: February 3, 2005 at 11:09:02 Pacific
Reply:

Sorry for not explaining at the begining, what i am trying to do is change a txt file into an exe file. I thought maybe I would have to read the text then output it.
just changing the file extension will do if you know how to do that.
thanx

djas


0

Response Number 3
Name: djas
Date: February 3, 2005 at 15:08:13 Pacific
Reply:

Exelent that works great but one other thing. What is the code to rename a file in c++.
thanx

djas


0

Response Number 4
Name: djas
Date: February 4, 2005 at 09:12:09 Pacific
Reply:

Its ok i figured it out using the

rename("oldname","newname");

function.

djas


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ code probs

Programming a proxy in C code www.computing.net/answers/programming/programming-a-proxy-in-c-code/6551.html

what does this C++ code do???!!?? www.computing.net/answers/programming/what-does-this-c-code-do/3230.html

c++ code !! www.computing.net/answers/programming/c-code-/4808.html