Output/Input to File Help

Can anyone help me with part of a program I'm making?
I just tried to make a program that would compare two different files and see if they were the same. I did this by first storing the lines of the file in an array and then compare the lines to each other individually in a loop. But then I met a problem with files that had blank lines in between text like:
a

b
which made an error when I the program tried to read it and put it in the array. Does anyone know if there is a simple way to make it work so that it will read blank lines? Or do I just have to somehow look through the files and get rid of all blank lines? Any help is really appreciated. Thanks in advance.
Last edited on
I most certainly can help. How exactly were you reading the file in? You can easily check for equality with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool fequal(fstream& first, fstream& second){
    //IF the current pointer pos is important, copy it here, and change them back before returning

    first.seekg (0, ios::beg);     //shoot the pointer to the start of the file
    second.seekg (0, ios::beg);

    while(!first.eof() && !second.eof()){
       char buf1 = first.get();     //get a single char from the file
       char buf2 = second.get();
       if (buf1 != buf2) //also does the eof byte.
          return false;
    }
    return true;
}
bool fequal(string first, string second){  //doesn't do any error handling
   fstream a;
   fstream b;
   a.open(first.c_str(),ios::in|ios::binary);
   b.open(second.c_str(),ios::in|ios::binary);
   bool ret = fequal(a,b);
   a.close();
   b.close();
   return ret;
}
I was reading the file like this but it seems alot different from the way you did it. I also don't really understand eof and when I goolged it, some people said that it almost always came up with errors resulting in an infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	string line; 
	string tempLine;
	//int amount = 0;
	int countT = 0;
	int countC = 0;
	int x = 0;
	vector <string> archiveT;
	vector <string> archiveC;
	ifstream tempFile ("Temporary.txt");
	if (tempFile.is_open())
	{
		
		
		while (tempFile.good())
		{
			getline (tempFile, tempLine);
			archiveT.push_back(tempLine);
		}
		
	}
        if (curFile.is_open())
	{
		
		while (curFile.good())
		{
			getline (curFile, tempLine);
			archiveC.push_back(tempLine);
		}
	}
        return 0;
}


Also is there a way to read all the contents of a file and just store it into one variable, without doing what I did where I took each line and stored it in a vector because that would really be helpful.
Last edited on
Topic archived. No new replies allowed.