Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 10-08-08   #1 (permalink)
Multi-Quote King
 
The Hundred Gunner's Avatar
 
amd nvidia

Join Date: Jul 2006
Posts: 8,672

Rep: 653 The Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famous
Unique Rep: 371
Folding Team Rank: 1269
Trader Rating: 0
Default Works in UNIX but not windows?

Can someone tell me why this program works in UNIX (OSX, more specifically) but not in windows? The goal of the program is to read a series of numbers from a file and report the largest and smallest ones. Here's the program in a nutshell:

1. Ask what file to use to input data
2. Check if the file exists
3. If so, check that the file actually contains numbers
4. If so, check what is the greatest number by comparing values
5. Check what is the smallest number by comparing the values

In UNIX, the program works flawlessly. When I copy the code to windows and build, the program works correctly EXCEPT for the reporting of the least number, I get garbage. I have no idea why this works in one OS, but in the other it works half-assed. Here's the code:

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

int main(void)
{
	long largest, smallest, temp;
	
	string file;	//For which file to use to import data
	
	int file_contains_values;
	
	cout << "Get data from which file? ";
	cin >> file;
	
	ifstream data_list;
	
	data_list.open(file.c_str());
	
	if (!data_list)	//If no file, exit program
	{
		cout << "The file \"" << file << "\" could not be read." << endl;
		cout << "The program will now exit.  Press any key to continue.\n";
		getchar();
		getchar();
		return 0;
	}
	
	else
	{
	
	if (data_list >> file_contains_values == 0)	//If read of values fails, program closes.
		{
			cout << "There is no data in " << file << ".  The program will now exit.\n";
			getchar();
			getchar();
			return 0;
		}
		
		else
		{
		data_list.close();	//Close the file and open again to start from beginning
		data_list.open(file.c_str());	/*b/c 1st value was used to determine if numbers existed,
											the file will be read from 2nd value otherwise*/
	
		cout << endl << "Checking for largest number...\n";
		data_list >> largest;
		
		do
		{
			data_list >> temp;
			
			if (temp > largest)
			largest = temp;		
		
		} while (!data_list.eof());
		data_list.close();	//Close the file
		
		cout << "The largest number in the list is " << largest << endl << endl;
		
		cout << "Checking for the smallest number...\n";
		data_list.open(file.c_str());
		data_list >> smallest;
		
		do
		{
			data_list >> temp;
			
			if (temp < smallest)
			smallest = temp;
		} while (!data_list.eof());
		
		cout << "The smallest number is " << smallest << endl << endl;
		data_list.close();
		}
		
	}
	
	return 0;
}
Any help is apreciated.
__________________
"But you would be amazed by how many people think that the only reason to have a computer is to play games, and that playing games is all that anyone with a computer does." - dangerousHobo

"Linux is everywhere, it is all around us, even now in this very room. You can see it when you look out your window, or you turn on your television. You can feel it when you go to work, when you go to church, when you pay your taxes. " - mbp

"I have used a mac before. And they still don't have right click I mean come on." - aakar

Blah, blah, can't hear you

System: The Shuttle Has Landed
CPU
Athlon 64 3800+ @2.785GHz
Motherboard
Shuttle
Memory
2GB OCZ
Graphics Card
7950GT
Hard Drive
320GB Seagate in Ext. Case
Sound Card
On-board
Power Supply
250W Shuttle "Elanpower"
Case
Shuttle Glamour xPC
CPU cooling
Shuttle "ICE2"
GPU cooling
Stock
OS
Censored to avoid flaming
Monitor
HP M70
The Hundred Gunner is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 10-08-08   #2 (permalink)
Overclocker
 
Manyak's Avatar
 
intel nvidia

Join Date: Mar 2008
Posts: 4,017

Rep: 328 Manyak is a proven memberManyak is a proven memberManyak is a proven memberManyak is a proven member
Unique Rep: 218
Folding Team Rank: 269
Trader Rating: 21
Default

You shouldn't keep closing and reopening the file over and over. Instead of doing that, why don't you try using:

Code:
seekg(ios_base::beg);
That will send the read pointer back to the beginning of the file. This will most likely fix the problem.
__________________
For Sale: A900, P4s, P3s, all kinds of RAM, HDDs, and more!!


Under Contrsuction: [Project] Ultimate TEC
Under Construction: [Project] Jack In The Box
On Hold: [Project] Wraith
On Hold: [Project] Belial

System: Gravehouse
CPU
Intel QX9650
Motherboard
Intel DX38BT
Memory
4GB OCZ Platinum DDR3-1333
Graphics Card
Geforce 8800gts G80
Hard Drive
2x WD RE2 500GB RAID0
Sound Card
X-Fi Platinum
Power Supply
ABZ Tagan 800W
Case
CM Stacker 832
Monitor
Sony GDM-FW900 CRT 2304x1440 @ 79Hz

Last edited by Manyak : 10-08-08 at 02:47 AM.
Manyak is offline I fold for Overclock.net   Reply With Quote
Old 10-08-08   #3 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

It could be that your assumption that your .close() - .open() sequence resetting the file pointer back to 0 is not correct on the system you are compiling on, or it could not be resetting the EOF flag. Try checking for EOF just before and just after the first time you read in smallest.

Or I could just be off my rocker


What compiler are you using?
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is offline I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 10-08-08   #4 (permalink)
Multi-Quote King
 
The Hundred Gunner's Avatar
 
amd nvidia

Join Date: Jul 2006
Posts: 8,672

Rep: 653 The Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famous
Unique Rep: 371
Folding Team Rank: 1269
Trader Rating: 0
Default

Quote:
Originally Posted by Manyak View Post
You shouldn't keep closing and reopening the file over and over. Instead of doing that, why don't you try using:

Code:
seekg(ios_base::beg);
That will send the read pointer back to the beginning of the file. This will most likely fix the problem.
That actually made UNIX reproduce the error that I would otherwise ONLY experience in windows

I actually tried a few things based on this reference:

http://www.cplusplus.com/reference/i...eam/seekg.html

I tried:

Code:
data_list.seekg(ios_base::beg);
and

[code]data_list.seekg(0, ios::beg);[/quote]

Both produce a garbage number for smallest, even though largest is given the correct value. The garbage value seems to be different for the two above.

Quote:
Originally Posted by The Bartender Paradox View Post
It could be that your assumption that your .close() - .open() sequence resetting the file pointer back to 0 is not correct on the system you are compiling on, or it could not be resetting the EOF flag. Try checking for EOF just before and just after the first time you read in smallest.

Or I could just be off my rocker


What compiler are you using?
If the EOF flag isn't being reset, how would I reset it?

The compiler I'm using in OSX is g++, and the compiler I'm using in windows is Visual C++ Express.
__________________
"But you would be amazed by how many people think that the only reason to have a computer is to play games, and that playing games is all that anyone with a computer does." - dangerousHobo

"Linux is everywhere, it is all around us, even now in this very room. You can see it when you look out your window, or you turn on your television. You can feel it when you go to work, when you go to church, when you pay your taxes. " - mbp

"I have used a mac before. And they still don't have right click I mean come on." - aakar

Blah, blah, can't hear you

System: The Shuttle Has Landed
CPU
Athlon 64 3800+ @2.785GHz
Motherboard
Shuttle
Memory
2GB OCZ
Graphics Card
7950GT
Hard Drive
320GB Seagate in Ext. Case
Sound Card
On-board
Power Supply
250W Shuttle "Elanpower"
Case
Shuttle Glamour xPC
CPU cooling
Shuttle "ICE2"
GPU cooling
Stock
OS
Censored to avoid flaming
Monitor
HP M70
The Hundred Gunner is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 10-08-08   #5 (permalink)
Overclocker
 
Manyak's Avatar
 
intel nvidia

Join Date: Mar 2008
Posts: 4,017

Rep: 328 Manyak is a proven memberManyak is a proven memberManyak is a proven memberManyak is a proven member
Unique Rep: 218
Folding Team Rank: 269
Trader Rating: 21
Default

Hrm...can you put a couple of cout statements at the beginning and middle of both the largest and smallest loops? This way we can see exactly when the garbage comes up.


Like this:
Code:
		cout << "Checking for the smallest number...n";
		data_list.open(file.c_str());
		data_list >> smallest;
		cout << "Initial: " << smallest << endl;
		do
		{
			data_list >> temp;
			cout << "Next Smallest: " << temp << endl;
			if (temp < smallest)
			smallest = temp;
		} while (!data_list.eof());
		
		cout << "The smallest number is " << smallest << endl << endl;
		data_list.close();
Its possible that the program is only reading garbage after a certain number of reads, and that garbage happens to be a really low negative number (bitwise), which is why it doesn't cause an error in the "largest" loop.

I'm actually betting now that the garbage read is only the last one.
__________________
For Sale: A900, P4s, P3s, all kinds of RAM, HDDs, and more!!


Under Contrsuction: [Project] Ultimate TEC
Under Construction: [Project] Jack In The Box
On Hold: [Project] Wraith
On Hold: [Project] Belial

System: Gravehouse
CPU
Intel QX9650
Motherboard
Intel DX38BT
Memory
4GB OCZ Platinum DDR3-1333
Graphics Card
Geforce 8800gts G80
Hard Drive
2x WD RE2 500GB RAID0
Sound Card
X-Fi Platinum
Power Supply
ABZ Tagan 800W
Case
CM Stacker 832
Monitor
Sony GDM-FW900 CRT 2304x1440 @ 79Hz

Last edited by Manyak : 10-08-08 at 03:51 AM.
Manyak is offline I fold for Overclock.net   Reply With Quote
Old 10-08-08   #6 (permalink)
Linux Lobbyist
 
error10's Avatar
 
intel nvidia

Join Date: Sep 2008
Location: Manchester, New Hampshire
Posts: 7,111
Blog Entries: 2

Rep: 772 error10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famous
Unique Rep: 488
Folding Team Rank: 40
Hardware Reviews: 1
Trader Rating: 17
Default

Why are you reading the file twice at all? Just read it once.

System: Underground
CPU
Core i7 920
Motherboard
EVGA X58
Memory
3x2GB Corsair Dominator DDR3-1600
Graphics Card
EVGA 9800 GT Superclocked 512MB
Hard Drive
WD1001FALS
Sound Card
Intel ICH10 HD Audio
Power Supply
OCZ StealthXStream 600W
Case
Thermaltake Armor VA8003BWS
CPU cooling
TRUE Lapped + Scythe Slip Stream 1900
GPU cooling
Stock
OS
Fedora 10 x86_64
Monitor
HP w19b
error10 is online now I fold for Overclock.net error10's Gallery   Reply With Quote
Old 10-10-08   #7 (permalink)
Multi-Quote King
 
The Hundred Gunner's Avatar
 
amd nvidia

Join Date: Jul 2006
Posts: 8,672

Rep: 653 The Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famous
Unique Rep: 371
Folding Team Rank: 1269
Trader Rating: 0
Default

Quote:
Originally Posted by error10 View Post
Why are you reading the file twice at all? Just read it once.
If I don't do the opening/closing, then the next time I want to read from the list, it doesn't start from the top. I need it to start from the top, and the only way I know how to do that is to close then open the file again.
__________________
"But you would be amazed by how many people think that the only reason to have a computer is to play games, and that playing games is all that anyone with a computer does." - dangerousHobo

"Linux is everywhere, it is all around us, even now in this very room. You can see it when you look out your window, or you turn on your television. You can feel it when you go to work, when you go to church, when you pay your taxes. " - mbp

"I have used a mac before. And they still don't have right click I mean come on." - aakar

Blah, blah, can't hear you

System: The Shuttle Has Landed
CPU
Athlon 64 3800+ @2.785GHz
Motherboard
Shuttle
Memory
2GB OCZ
Graphics Card
7950GT
Hard Drive
320GB Seagate in Ext. Case
Sound Card
On-board
Power Supply
250W Shuttle "Elanpower"
Case
Shuttle Glamour xPC
CPU cooling
Shuttle "ICE2"
GPU cooling
Stock
OS
Censored to avoid flaming
Monitor
HP M70
The Hundred Gunner is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 10-10-08   #8 (permalink)
Linux Lobbyist
 
error10's Avatar
 
intel nvidia

Join Date: Sep 2008
Location: Manchester, New Hampshire
Posts: 7,111
Blog Entries: 2

Rep: 772 error10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famouserror10 is becoming famous
Unique Rep: 488
Folding Team Rank: 40
Hardware Reviews: 1
Trader Rating: 17
Default

Yeah, but the point is, you only NEED to read the file once!

System: Underground
CPU
Core i7 920
Motherboard
EVGA X58
Memory
3x2GB Corsair Dominator DDR3-1600
Graphics Card
EVGA 9800 GT Superclocked 512MB
Hard Drive
WD1001FALS
Sound Card
Intel ICH10 HD Audio
Power Supply
OCZ StealthXStream 600W
Case
Thermaltake Armor VA8003BWS
CPU cooling
TRUE Lapped + Scythe Slip Stream 1900
GPU cooling
Stock
OS
Fedora 10 x86_64
Monitor
HP w19b
error10 is online now I fold for Overclock.net error10's Gallery   Reply With Quote
Old 10-10-08   #9 (permalink)
Overclocker
 
Manyak's Avatar
 
intel nvidia

Join Date: Mar 2008
Posts: 4,017

Rep: 328 Manyak is a proven memberManyak is a proven memberManyak is a proven memberManyak is a proven member
Unique Rep: 218
Folding Team Rank: 269
Trader Rating: 21
Default

Quote:
Originally Posted by The Hundred Gunner View Post
If I don't do the opening/closing, then the next time I want to read from the list, it doesn't start from the top. I need it to start from the top, and the only way I know how to do that is to close then open the file again.
He means that as you read it the first time, check both variables at once.

Code:
		data_list.open(file.c_str());
		if (!(data_list >> temp))
		{
			cout << "There is no data in " << file << ".  The program will now exit.n";
			return 0;
		}
		
		// If the program didn't exit, then temp will already contain the first entry
		// So put the initial value into both smallest and largest
		// And check both in one loop
		smallest = temp;
		largest = temp;
		
		do {
			data_list >> temp;

			if (temp < smallest)
				smallest = temp;

			if (temp > largest)
				largest = temp;
		} while (!data_list.eof());
		
		cout << "The smallest number is " << smallest << endl;
		cout << "The largest number is " << largest << endl;
		data_list.close();
This way you only have to read the file once.
__________________
For Sale: A900, P4s, P3s, all kinds of RAM, HDDs, and more!!


Under Contrsuction: [Project] Ultimate TEC
Under Construction: [Project] Jack In The Box
On Hold: [Project] Wraith
On Hold: [Project] Belial

System: Gravehouse
CPU
Intel QX9650
Motherboard
Intel DX38BT
Memory
4GB OCZ Platinum DDR3-1333
Graphics Card
Geforce 8800gts G80
Hard Drive
2x WD RE2 500GB RAID0
Sound Card
X-Fi Platinum
Power Supply
ABZ Tagan 800W
Case
CM Stacker 832
Monitor
Sony GDM-FW900 CRT 2304x1440 @ 79Hz

Last edited by Manyak : 10-10-08 at 02:17 AM.
Manyak is offline I fold for Overclock.net   Reply With Quote
Old 10-10-08   #10 (permalink)
Multi-Quote King
 
The Hundred Gunner's Avatar
 
amd nvidia

Join Date: Jul 2006
Posts: 8,672

Rep: 653 The Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famousThe Hundred Gunner is becoming famous
Unique Rep: 371
Folding Team Rank: 1269
Trader Rating: 0
Default

Yeah, a friend ended up telling me that I could have just done it all at once... But I'm still stumped as to what's making it F up in windows but it works just fine in UNIX. My class TA is stumped as well.

Anyway, thanks to you all.
__________________
"But you would be amazed by how many people think that the only reason to have a computer is to play games, and that playing games is all that anyone with a computer does." - dangerousHobo

"Linux is everywhere, it is all around us, even now in this very room. You can see it when you look out your window, or you turn on your television. You can feel it when you go to work, when you go to church, when you pay your taxes. " - mbp

"I have used a mac before. And they still don't have right click I mean come on." - aakar

Blah, blah, can't hear you

System: The Shuttle Has Landed
CPU
Athlon 64 3800+ @2.785GHz
Motherboard
Shuttle
Memory
2GB OCZ
Graphics Card
7950GT
Hard Drive
320GB Seagate in Ext. Case
Sound Card
On-board
Power Supply
250W Shuttle "Elanpower"
Case
Shuttle Glamour xPC
CPU cooling
Shuttle "ICE2"
GPU cooling
Stock
OS
Censored to avoid flaming
Monitor
HP M70
The Hundred Gunner is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools