Hey,

Does anyone know how to cpy from one file and redirect to another. I know I have to use the ofstream but I'm stuck.

while (!in1.eof())	                             // while not end of input file
   {
	in1.getline(current, 100);	                 // read a character from input file
	 out1.in
   }

I'm stuck. I want to use a while loop to get the line then place it into another file until I've reached end of file.

There is a reason why I'm using getline(), I need the Carriage return (0D) to be present. Some file only have CRLF(0A) and I know getline will place the 0D in the right place thus I'm doing it line by line. Just can't seem to figure out how to redirect the file into another file

Edited 5 Years Ago by ana_1234: n/a

Does anyone know how to cpy from one file and redirect to another.

#include <fstream>

using namespace std;

int main()
{
    ifstream in("source", ios::binary);
    ofstream out("destination", ios::binary);

    if (in)
        out << in.rdbuf();
}

I need the Carriage return (0D) to be present.

Text mode streams will convert whatever platform dependent line break method is used into '\n' on reading and back on writing. Your requirement makes no sense to me, but if you want an exact copy of a file, open the streams in binary mode.

Edited 5 Years Ago by Narue: n/a

This is the code. I'm going through files and some have both carriage return and CRLF, other just have CRLF(which are the ones I'm trying to fix). I assummed if I used get line to retrieve the information from the file and print them to another file then that would take care of the CRLF. My question does anyone know a way of copying from one file to another. Mine is not reading it correctly.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void openFile (ofstream &, ifstream &, char[]);
bool Check0d (ofstream &, ifstream &);

int main()
{
   char filename[100];   
   char current[100];
   bool Stats;

   ifstream in1;	        // input file stream
   ofstream out1;	        // an output file stream
 
  openFile (out1, in1, filename);
  Stats = Check0d(out1,in1);
   
	out1.open(filename, ios::binary);
	 if (Stats == true)
	 {
		 while (!in1.eof())	                             // while not end of input file
		 {
			 in1.getline(current, 100);	                 // read a character from input file
			 out1 << current << endl;
		 }
	 }

	 return 0;
}

void openFile (ofstream& out1, ifstream& in1, char filename[])

{
	  do
   {
      in1.clear();
      cout << "Please enter the name of the input file.\n";
      cout << "Filename:  ";
      cin >> setw(25) >> filename;

	  in1.open(filename, ios::binary);					 // allows it to open in binary mode
      if (!in1)
         cout << "That is not a valid file.  Try again!\n";

   } while (!in1);

  /* do
   {
      out1.clear();
      cout << "Please enter the name of the output file.\n";
      cout << "Filename:  ";
      cin >> setw(25) >> filename;

      out1.open(filename);
      if (!out1)
         cout << "That is not a valid file.  Try again!\n";

   } while (!out1);*/
}
bool Check0d (ofstream& out1, ifstream& in1)
{
	char current;
	char previous =  0x00;   //sets previous to 0
    int fix = 0;
	int notfix = 0;
	bool status = false;
	
	while (!in1.eof())	                             // while not end of input file
	  {
		  in1.get(current);	                             // read a character from input file
		  
		  if (previous != 0x0d && current == 0x0A)    //comparison used to see if the file has an 0a, if file had an 0a then status because true
		  {
			  status = true;
			  break;
		  }
		  
		  previous = current;								 // sets previous to the current digit so as the loop starts again, current is the next input and previous is the current before
	}
	
	return status;
}

Edited 5 Years Ago by ana_1234: n/a

My question does anyone know a way of copying from one file to another.

Are you reading impaired? I gave you a short program that performs an exact copy of a file. All you need to do is incorporate that into your code, and nothing extra needs to be done to handle rogue carriage returns.

Edited 5 Years Ago by Narue: n/a

Comments
+

Click on the file, hit "Ctrl + C" and goto the location where you want to paste it and hit "Ctrl + V" where the '+' means together or in conjunction.

#include <fstream>

using namespace std;

int main()
{
    ifstream in("source", ios::binary);
    ofstream out("destination", ios::binary);

    if (in)
        out << in.rdbuf();
}

Text mode streams will convert whatever platform dependent line break method is used into '\n' on reading and back on writing. Your requirement makes no sense to me, but if you want an exact copy of a file, open the streams in binary mode.

Your reading must be wrong, because what you sent doesnt work

because what you sent doesnt work

"It doesn't work" is a code phrase for "I don't know how to use it". So how about you post your code that "doesn't work" and I'll show you where you made a mistake.

I fixed that problem I have another issue, but your code didn't copy anything over. thanks though

Edited 5 Years Ago by ana_1234: n/a

Your reading must be wrong, because what you sent doesnt work

Are you kidding me? Really? Did you check the file names? Why not try making the if condition this: if(in && out) Oh and it would make your code look much more professional if you took better care of the indentation. If you're using Visual Studio you can select a block of code and indent it left or right, which is a very awesome feature. You can also comment a selected block of code and probably do other things.

Are you kidding me? Really? Did you check the file names? Why not try making the if condition this: if(in && out) Oh and it would make your code look much more professional if you took better care of the indentation. If you're using Visual Studio you can select a block of code and indent it left or right, which is a very awesome feature. You can also comment a selected block of code and probably do other things.

Its not that is doesnt work. I fixed the copying part but I'm tryning to fina a way to copy it where the files without the carriage return(0D) will have it.

The guys way didnt even copy. The Hexidecimal comparison print and extra carriage return on the copied file.

for example, this is a sample:

This is some of the hexidecimal version of the original

23 69 66 20 21 64 65 66 69 6E 65 64 20 54 45 53 54 5F 48 0A 23 64 65 66 69 6E 65 20 54 45 53 54 5F 48 0A 0A

and I need it to look like this

23 69 66 20 21 64 65 66 69 6E 65 64 20 54 45 53 54 5F 48 0D 0A 23 64 65 66 69 6E 65 20 54 45 53 54 5F 48 0D 0A 0D 0A

but it keeps printing like this an extra 0D

23 69 66 20 21 64 65 66 69 64 20 54 45 53 54 5F 48 0D 0D 0A 23 64 65 66 69 20 54 45 53 54 5F 48 0D 0D 0A 0D 0D 0A

I know how to fix it manually through Notepad2, but my task is to fix it automatically

Edited 5 Years Ago by ana_1234: n/a

The guys way didnt even copy

Because you clearly didn't understand how simple it can be:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;

void openFile (ifstream& in, char filename[]);

int main()
{
    char filename[100];

    ifstream in;

    openFile(in, filename);
    strcat(filename, ".txt");

    ofstream out(filename, ios::binary);

    out << in.rdbuf();

    return 0;
}

void openFile (ifstream& in, char filename[])
{
    do
    {
        in.clear();

        cout << "Please enter the name of the input file.\n";
        cout << "Filename:  ";
        cin >> setw(25) >> filename;

        in.open(filename, ios::binary);

        if (!in)
            cout << "That is not a valid file.  Try again!\n";
    }
    while (!in);
}

I fixed the copying part but I'm tryning to fina a way to copy it where the files without the carriage return(0D) will have it.

So you want to ensure that newlines are CRLF even if they weren't in the original file?

Edited 5 Years Ago by Narue: n/a

Because you clearly didn't understand how simple it can be:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;

void openFile (ifstream& in, char filename[]);

int main()
{
    char filename[100];

    ifstream in;

    openFile(in, filename);
    strcat(filename, ".txt");

    ofstream out(filename, ios::binary);

    out << in.rdbuf();

    return 0;
}

void openFile (ifstream& in, char filename[])
{
    do
    {
        in.clear();

        cout << "Please enter the name of the input file.\n";
        cout << "Filename:  ";
        cin >> setw(25) >> filename;

        in.open(filename, ios::binary);

        if (!in)
            cout << "That is not a valid file.  Try again!\n";
    }
    while (!in);
}

So you want to ensure that newlines are CRLF even if they weren't in the original file?

To the question above yes. Thanks for you help sir, but that has the same hexidecimal comparison as the original.

To the question above yes.

Okay, that's not a copy, it's a translation. The problem is that you can't use getline to do it because getline doesn't recognize newline configurations that aren't for the compiler's target platform. In other words, getline will recognize CRLF on Windows, but not just CR or just LF. So you won't get the correct splitting of lines.

This is a case where the translation is rather manual on two binary streams:

#include <fstream>

using namespace std;

int main()
{
    ifstream in("source", ios::binary);

    if (in) {
        ofstream out("destination", ios::binary);
        char ch;

        while (in.get(ch)) {
            if (ch != '\r')
                out.put(ch);
            else {
                // Assume all carriage returns mark a newline
                out << "\r\n";

                // Trim a line feed because we just wrote it
                if (in.peek() == '\n')
                    in.ignore();
            }
        }
    }
}
This article has been dead for over six months. Start a new discussion instead.