Does anyone know if there is a function in the fstream.h library that will allow me to copy the contents of one file to another one with just a different file name?
Does anyone know if there is a function in the fstream.h library that will allow me to copy the contents of one file to another one with just a different file name?
No, but it's fairly simple to do.
-open both files
-if both opened successfully:
loop:
-read a byte from source file
-if not EOF:
-write the byte to destination file
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Here is the code I was trying to use to copy the file.
I can't get it to work.Code:#include <iostream.h> #include <fstream.h> int main() { ifstream InFile("source.txt", ios::nocreate); if (InFile.fail()){ cout << "Source.txt could not be opened"; } ifstream InFile("src.txt", ios::nocreate); if (InFile.fail()){ InFile("src.txt", ios::create); cout << "src.txt was not found and has been created"; } return(0); else { char Character; while (InFile.get(Character)){ if (inFile.eof()){ cout << "File Copied" << endl; } else{ ofstream OutFile("src.txt"); cout << "Copying File contents" << endl; } } } return (0); }
A lot of logical errors in your code...I'm surprised it even compiled, what with the return statement between the first if/else.
The next most glaring problem is you are not copying the byte to the new file.
Here's a sample C function:
Code:/* * return codes: * -1 (Source file not found) * -2 (Dest file couldn't be opened) * >= 0 (Number of bytes copied) */ int copy_file(const char destination[], const char source[]) { int byte, result = 0; FILE * in, * out; in = fopen(source, "r"); if(in == NULL) result = -1; else { out = fopen(destination, "w"); if(out == NULL) result = -2; else { while(EOF != (byte = fgetc(in))) { fputc(byte, out); ++result; } fclose(out); } fclose(in); } return result; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
I made some changes to correct some of the errors I was getting during the compile. I read the code that was posted but I still can't figure out what I am doing wrong. Here is the new code
Code:#include <iostream.h> #include <fstream.h> int main() { ifstream InFile("source.txt", ios::nocreate); if (InFile.fail()){ cout << "Source.txt could not be opened"; } ifstream InFile("src.txt", ios::nocreate); if (InFile.fail()){ InFile("src.txt"); cout << "src.txt was not found and has been created"; } else { char Character; while (InFile.get(Character)){ if (InFile.eof()){ cout << "File Copied" << endl; } else{ ofstream OutFile("src.txt"); cout << "Copying File contents" << endl; } } } return (0); }
You may want to try something like this :
Code:#include <iostream.h> #include <fstream.h> ifstream InFile; ofstream OutFile; char one; int main() { InFile.open("source.txt", ios::in | ios::nocreate); if(InFile) { OutFile.open("write.txt", ios::out); while(InFile.get(one)) { while(one != EOF) { OutFile << one; } } InFile.close(); OutFile.close(); } }
I've made a LOT of mistakes here, but I've just come back from a party However, I'm sure you get the point : you will need to open the output file using ofstream and not ifstream. You also may need to open it using ios::binary, I'm not sure. Otherwise, I think that's pretty much it.