|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Works in UNIX but not windows?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Multi-Quote King
|
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;
}
__________________
"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
|
|||||||||||||
|
|
|
|
#2 (permalink) | ||||||||||
|
Overclocker
|
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);
__________________
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
Last edited by Manyak : 10-08-08 at 02:47 AM. |
||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
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?
__________________
Congratulations! You have found the secret text! You get a cookie.
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||||
|
Multi-Quote King
|
Quote:
![]() 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); [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:
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
|
|||||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||
|
Overclocker
|
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();
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
Last edited by Manyak : 10-08-08 at 03:51 AM. |
||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Linux Lobbyist
|
Why are you reading the file twice at all? Just read it once.
__________________
For Sale: NEW Unreal Tournament 3 -- NEW Assassin's Creed -- Case Badges I did error10's Windows Challenge and I now am an MCSE: Minesweeper Consultant and Solitaire Expert! ![]()
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
Multi-Quote King
|
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
|
|||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Linux Lobbyist
|
Yeah, but the point is, you only NEED to read the file once!
__________________
For Sale: NEW Unreal Tournament 3 -- NEW Assassin's Creed -- Case Badges I did error10's Windows Challenge and I now am an MCSE: Minesweeper Consultant and Solitaire Expert! ![]()
|
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||
|
Overclocker
|
Quote:
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();
__________________
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
Last edited by Manyak : 10-10-08 at 02:17 AM. |
|||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||
|
Multi-Quote King
|
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
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |