Files,

Inputting data into arrays

Page 1 of 1

1 Replies - 393 Views - Last Post: 14 December 2008 - 12:27 AM Rate Topic: -----

#1 rady  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-November 08

Files,

Post icon  Posted 13 December 2008 - 11:59 PM

Hello,
I have to compare two files, and I was thinking that in order to do that I have to input them into arrays. So, When I try doing that by reading the files line by line it does not work (using getline(myfile,x)). So I think I will change it to myfile.get(x). So my problem is how from that point can I input the file into an array, and how do I compare the lines between the files.
Thank you
#include <conio.h>	
#include <iostream> 
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	string filename;
	cout << "Enter filename: ";
	cin >> filename;
	cout << endl;	

	ifstream myFile;
	myFile.open(filename.c_str());
	if (myFile.fail())
	{
		cout << "Failed to open the file: ";
		cout << filename << endl;
	}
	else
	{	
		string filename2;
		cout << "Enter filename: ";
		cin >> filename2;
		cout << endl;

		ifstream myFile2;
		myFile2.open(filename2.c_str());
		if (myFile2.fail())
			{
				cout << "Failed to open the file: ";
				cout << filename2 << endl;
			}
		else
		{
			string line;
			string line1;
			int i = 0;
			int count = 0;
			int count2 =0;
				do
					{			
						getline(myFile,line);
						cout << setw(3) << ++i << " * ";
						cout << line <<endl;
						count++;
					} while (!myFile.eof());					
					
					cout << endl;
					cout << "The second text is: "<< endl;
					cout << endl;
					i=0;
					do
					{	
						getline(myFile2,line1);
						cout << setw(3) << ++i << " ** ";
						cout << line1 <<endl;
						count2++;
					} while (!myFile2.eof());

					cout << endl;
					cout <<"The number of lines the first text has is: " << count;
					cout << " and the second text has: " << count2 << " lines ";
					
					const int size = 1000;
					string list[size];					
					string list1[size];
					list[1000]=line+line;
					list1[1000]=line1+line1;

					cout << line;
					cout << line1;



		}
		myFile.close();
		myFile2.close();
		cout << endl;


	}

	cout << "\nHit any key to quit" << endl;
	_getch();
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Files,

#2 lanec42  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 229
  • Joined: 26-March 08

Re: Files,

Posted 14 December 2008 - 12:27 AM

Try treating the file stream as binary, then read one memory block at a time. That would prevent any problems with overly large files. (Reading an entire file into memory is pretty insecure.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1