|
Replace:
if (data==data2){
by that:
if (strcmp(data,data2) == 0){
What you are doing is comparing the two pointers which are of course different (the pointer data points to another location than the pointer data2).
|
|
|
|
|
Thank u for the help. I replaced that but i still can´t see the messagebox with USB registered so is any problem because i know that the information of the first file(data) is in the other file(data2), for sure. So i dont know what to do. Thank u.
|
|
|
|
|
densitet wrote:
std::ifstream database ("C:\Documents and Settings\Densitet\My Documents\Visual Studio Projects\IOCP-SRC\IOCP-SRC\IOCP\Debug\USBDevices.txt");
This won't work with single backslahes in a string literal.
Cedric has already given you a solution, but I have question. Since this is an MFC application, why are you using char instead of CString ? By changing data and data2 to CString , you can then do the more natural if (data == data2) comparison.
Also, for each line that you are reading from the fileusb object, you are looking for that line in the database object. That works fine for the first pass through the database object but I don't see where that object gets reset so that it can be searched again for the next line in the fileusb object.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
ok i understand
so now i have this code:
char data[128];
std::ifstream fileusb ("GUIDs_found.txt");
char data2[128];
std::ifstream database ("C:\\USBDevices.txt");
bool salida2 =false;
while (!fileusb.eof() )
{
fileusb.getline(data,128);
database.getline(data2,128);
while (!database.eof()||(salida2!=true))
{
if (strcmp(data,data2) == 0){
salida2=true;
AfxMessageBox("USB registered");
break;
}
else{
fileusb.getline(data,128);
}
}
salida2=false;
}
fileusb.close();
database.close();
It is only a function that i added to my program, and it works for the first case but how i can reset fileusb because i want that it starts again to compare with all the lines that i have in the database, because when fileusb finish with all the lines comparing with the first element of database, databse changes to the second line, but fileusb doesnt reset.
Thank u for the help
|
|
|
|
|
Try the istream::seekg() method.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
dude I have no ideea why you complicate so much.
in a coule of line you couldcompre very huge txt files
CreateFile()
CreateFileMapping()
MapViewOfFile()
memcmp()
good luck
gabby
|
|
|
|
|
Which would not produce the desired result. Searching through one file for occurrences of strings contained in another file is not as simple as using memcmp() because the files are in no way guaranteed to be the same. They could be different sizes and could be ordered differently.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
if the file is small enough he could use
StrStrI(mapviewoffile1,mapviewoffile2)
otherwise:
if (size(f1)==size(f2))
while (!feof(f1)||!feof(f2))
{
if (fgetc(f1)!=fgetc(f2))
{
resemblance=FALSE;
break;
}
}
if(!resemblance)
{}
all this code in a thread.
gabby
|
|
|
|
|
Go back and read the initial post again. One text file will contain the USB devices for a particular machine. Another text file, the one stored on the server, will contain a much larger list of the registered USB devices to search through. There is no way you can reliably use StrStrI() or fgetc() for this type of comparison.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Extactly, my fileusb can has 2, 3, 4 usb devices plugged. And my database can has maybe 10 usb devices registered. So im getting crazy because i dont know how to use correctly istream::seekg(); and where exctaly, i suposse when i find one usb registered then reset with this function fileusb and i start in the beginning again and i compare with the second od the dataserver??, so i only have to write this function and the rest is ok??.. Thank u very much for the help
void compareFiles(){
char data[40];
std::ifstream fileusb ("GUIDs_found.txt");
char data2[40];
std::ifstream database ("C:\\USBDevices.txt");
bool salida2 =false;
while (!fileusb.eof() )
{
fileusb.getline(data,40);
database.getline(data2,40);
while (!database.eof()||(salida2!=true))
{
if (strcmp(data,data2) == 0){
salida2=true;
//data[40]=0;
AfxMessageBox("USB registered");
break;
}
else{
fileusb.getline(data,128);
}
}
salida2=false;
}
fileusb.close();
database.close();
}
|
|
|
|
|
Sorry but with what parameters i do the seekg because im looking the msdn and i dont have nothing clear.
|
|
|
|
|
look this:
CreateFile()
CreateFileMapping()
MapViewOfFile()
memcmp()
good luck
gabby
|
|
|
|
|
ifstream::seekg() works. If you are reseting to the beginning of the file, you can just close and open the file again.
Danny
|
|
|
|
|
Ok, now i have that, i have changed things that u told me, but now i think it is in a infinitive loop. Im new in c++ but a simple comparation to see if for example 2 elements from one file are in other file with 10 is difficult right now because i can't do a breakpoints to see where is the error. I dont know if u have any ideas for this code that i show here but the idea is that i described. thank u for all the help.
char data[128];
char data2[128];
bool salida2 = false;
ifstream fileusb;
ifstream database;
fileusb.open("GUIDs_found.txt", ios::in);
database.open("USBDevices.txt", ios::in);
fileusb.getline(data,128);
while (!fileusb.eof())
{
database.getline(data2,128);
while ((!database.eof())||(salida2!=true))
{
if (strcmp(data,data2) == 0){
salida2=true;
//database.clear();
database.seekg(0,ios::beg);
database.close();
database.open("USBDevices.txt", ios::in);
database.getline(data2,128);
fileusb.getline(data,128);
}
else{
database.getline(data,128);
}
}
salida2 = false;
}
fileusb.close();
database.close();
|
|
|
|
|
How about:
fileusb.getline(data, 128);
while (! fileusb.eof())
{
database.getline(data2, 128);
while (! database.eof() && salida2 != true)
{
if (strcmp(data, data2) == 0)
salida2 = true;
else
database.getline(data, 128);
}
database.seekg(0, ios::beg);
fileusb.getline(data, 128);
}
database.close();
fileusb.close();
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Ok, think i solved that, but when i find the first elemente and i change for the second in the first file i see a messagebox without nothing and later i start again good and when for example the second element is founded in the file i can't show USB registered i only show nothing but with the first i see USB registered,Maybe is the buffer??
fileusb.open("GUIDs_found.txt", ios::in);
database.open("USBDevices.txt", ios::in);//
while (! fileusb.eof())
{
fileusb.getline(data, 500);
AfxMessageBox(data);
database.getline(data2, 500);
AfxMessageBox(data2);
while (! database.eof() )
{
if (strcmp(data, data2) == 0){
//salida2 = true;
AfxMessageBox("USB registered");
break;
}
else if (strcmp(data,data2) != 0){
database.getline(data2, 500);
AfxMessageBox("USB NOT registered");
AfxMessageBox(data2);
}
}
database.seekg(0, ios::beg);
fileusb.getline(data, 500);
AfxMessageBox(data);
}
database.close();
fileusb.close();
|
|
|
|
|
Too many calls to getline() . Try:
fileusb.open("GUIDs_found.txt", ios::in);
database.open("USBDevices.txt", ios::in);
while (! fileusb.eof())
{
fileusb.getline(data, 500);
AfxMessageBox(data);
while (! database.eof())
{
database.getline(data2, 500);
AfxMessageBox(data2);
if (strcmp(data, data2) == 0)
{
AfxMessageBox("USB registered");
break;
}
}
database.seekg(0, ios::beg);
}
database.close();
fileusb.close();
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
And don't forget to put a check for the existence of the files after the open statement!
<br />
fileusb.open("GUIDs_found.txt", ios::in);<br />
database.open("USBDevices.txt", ios::in);<br />
if(!fileusb){<br />
return;<br />
}<br />
if(!database){<br />
return;<br />
}<br />
Even though it adds many lines of code, you should always check for errors... usually.
Danny
|
|
|
|
|
bugDanny wrote:
Even though it adds many lines of code, you should always check for errors... usually.
Of course, but that's not related to the problem(s) that densitet is having.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-- modified at 17:07 Wednesday 14th September, 2005
|
|
|
|
|
DavidCrow wrote:
Of course, but that's not related to the problem(s) that densitet is having.
Unless, of course, sometime (now or in the future) one of those files (for some unknown reason) does not exist where they were expected to be. In which case, the program would be stuck in an infinite loop, since it will never find eof() of the missing file. And an infinite loop is exactly the problem densitet was having.
Danny
|
|
|
|
|
bugDanny wrote:
And an infinite loop is exactly the problem densitet was having.
At one point in time, yes, but it was not because of a non-existent file.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Can any one help me. I have one problem in my MFC application. After an hour the application gets slowed down.That is after continuous use of the application about an hour it slows down itself and other applications too.
The CPU usage also seems to be good. It takes just 20 to 30%.
Checked with memory leak procedure. Seems to be okay. Is there any other methods by which the application can be monitored for memory leak and other things.
This is first time ever I see the problem.
Reply will be highly appreciable.
NSS
|
|
|
|
|
plz tell
what type of app is this ?
r u constantly adding variable dynamic stack of you application
|
|
|
|
|
The application is for PLC system. The PLC system keeps sending information about the status of control equipment. similarly the user can send request and commands to PLC.
If commands are sent frequently,the application gets slowed down. The application is well designed. There will two threads for PLC one thread for executing boolean equations.
What I suspect is, if commands are given during execution of other task, it delays the commands.
|
|
|
|
|
nss_75 wrote:
After an hour the application gets slowed down.
What are you using to gauge this?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|