For those of you who don't know I have been working on this damned file comparator for a little over a week now, and it's been driving me insane. I didn't want to ask for help, but I've run up shit creek. It all come down to this one part. I've narrowed the entire problem down to this one infernal line of code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int CompareFiles(fstream &file1, fstream &file2);
int main()
{
char* file[2] = {"",""};
char files[50] = "";
// Starting message
cout << "Enter: File1,File2\n";
cin >> files;
//Print what was entered
cout << endl <<files << endl;
// Will try to parse the text into different files
int p = 0;
for(int i = 0; files[i]; i++)
{
if(files[i]!=',')
{
file[p]+=files[i]; // Right here!
cout << file[p] << endl;
}
// The first file should print here
else {p++; cout << "This should be were the first is: " << file[p-1] <<endl;}
}
cout << file[0] << " " << file[1];
system("pause");
return 0;
}
Look at that it seems so damned easy right? All it's supposed to do is split up the entered text and add the strings to an array.
But every damned time I try to mess with this the thing blows up in my face. The part I highlighted in red is where everything Blows up. I can't figure out why o_O?
Here's the output:
I am so confused -_-
PS: At BA. I know you showed me one way to do it, but I want to understand how to do it this way before I try to do it the easy way.
file[p] is either char* file[0] or char* file[1] that points to a spot in memory that is your emtpy string ""
Let's start simply by saying p =0;
you say: File[0] += files[i];
So what does that do?
File[0] is a Pointer
Files[i] is a Character;
So you're adding a character (the VALUE of whatever letter is) in files[i] to Pointer File[0]
Say you enter 'a,b' and File[0] Pointers to a memory address of 0x00001000
The Hex Value for 'a' is 0x61.
So you end up saying file[0] = file[0] + 0x61; so you end up with file[0] pointer to memory address 0x000010061 where you point to garbage.
Of course the longer your actual entered filename is, it keeps moving the pointer further and further into memory
If you want to keep 2 pointers pointing to the different filenames inside the original string
(which is what you're doing)the thing to remember then is File[0] is always going to be &Files[0];
and File[1] is going to be pointing to the first character after the ','. you WILL need to replace the comma with a '\0' though otherwise File[0] will think it's the entire string length
Originally Posted by B1ackAnge1
you say: File[0] += files[i];
So you end up saying file[0] = file[0] + 0x61; so you end up with file[0] pointer to memory address 0x000010061 where you point to garbage.
Of course the longer your actual entered filename is, it keeps moving the pointer further and further into memory
Oh damn! That's not good o_O...
So what if I do this *file[0] += files[i];
....hmm weird dupe post...
That won't work either
I think you need to look back at a higher level what you're trying to accomplish:
Are you trying to set 2 pointers to the beginning of each filename? or are you are trying to COPY the names into secondary buffers/strings?
The 1st I sorta described above (added possibly after you responded)
the second you'd have another 2 char[50] or so buffers and their own index that you increment yourself so it copies over the characters from the other string
i don't understand pointers to arrays yet but found an other way to solve your "problem" (or i don't understand your problem)