How to use C + + + line reading TXT file string (can be conveniently implemented text file replication) __c++

Source: Internet
Author: User

This code has been tested under windows/vc++6.0, and there is no problem under linux/g++.

but be sure to be aware of the differences between Linux and Windows file formats, For example:

1. When code on Linux reads the Windows file format, there will be one more per row for reading the result. \ r, think about why.

2. When code on Windows reads a Linux format file, the read results show only one line and think about why.


First in C language to write an ugly program:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
	FILE *fp;
	if (NULL = = (fp = fopen ("1.txt", "R")))
	{
		printf ("error\n");
	    Exit (1);
	}

	char ch;
	while (EOF!= (CH=FGETC (FP))
	{
		printf ("%c", ch);
	}

	Fclose (FP);

	return 0;
}

You can only see the result, but you can't use every line. We will instead:

vc++6.0

#include <stdio.h>
#include <string.h>

int main ()
{
	char sztest[1000] = {0 };
	int len = 0;

	FILE *FP = fopen ("1.txt", "R");
	if (NULL = fp)
	{
		printf ("Failed to open dos.txt\n");
		return 1;
	}

	while (!feof (FP))
	{
		memset (sztest, 0, sizeof (sztest));
		Fgets (sztest, sizeof (sztest)-1, FP); Contains a newline character
		printf ("%s", sztest);
	}

	Fclose (FP);

	printf ("\ n");

	return 0;
}

So we just read the whole row.

feel C reading method a bit ugly, or see C + + bar (as long as the file format Windows/linux and compile platform windows/linux correspondence, you can rest assured that use it):

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

int main ()
{
	ifstream in ("1.txt");
	string filename;
	string line;

	if (in)//There is a
		newline character
		{ 
			cout << line << endl;
  
   
	/else//does not have the file
	{
		cout << "No such file" << Endl;
	}

	return 0;
}
  

Of course, you can modify the above program so that each line in 1.txt is entered into 2.txt, as follows:

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

int main ()
{
	ifstream in ("1.txt");
	Ofstream out ("2.txt");
	string filename;
	string line;

	if (in)//There is a
		newline character
		{ 
			cout << line << endl;
  out << line << Endl; Input to 2.txt
		}
	else//does not have the file
	{
		cout << "No such file" << Endl;
	}

	return 0;
}

As a result, the content of 2.txt and 1.txt is exactly the same, you can compare it with beyond compare, I compare it.

It seems that the above procedures can also achieve the duplication of documents, as follows:

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

void FileCopy (char *file1, char *file2)
{
	//is best judged on file1 and File2
	
	ifstream in (file1);
	Ofstream out (file2);
	string filename;
	string line;

	while (Getline (on)) {out 
		<< line << Endl;
	}
}

int main ()
{
	filecopy ("1.txt", "2.txt");
	return 0;
}

Of course, the above program is only for text files (not just. txt), not for other types of files.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.