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

Reply
 
LinkBack Thread Tools
Old 4 Weeks Ago   #1 (permalink)
AMD Overclocker
 
NameUnknown's Avatar
 
amd ati

Join Date: Apr 2009
Location: Ohio
Posts: 1,060

Rep: 45 NameUnknown is acknowledged by some
Unique Rep: 38
Trader Rating: 1
Default C++ issues reading from files

Ive never been good at reading from files in C++, but at the same time Ive never had this much trouble with it either. And Im almost guaranteed to be overlooking a stupid and simple issue with the code, but whatever it is, I cant find it.

On to the issues with this program though. for this program i have to read in values from a .txt file and then add 10 to each value and output it all into a new file. Ive got it outputting fine, but it wont read write. All I get depending on how I write it is 0, 10, or -858993450, never the right answer.

As you can see, it initially checks to make sure it isnt at the end of the file, and if it is it exits, if not, it goes on to do the calculations. Well as said above the calculations dont give any real results, and then it just sits in an infinite loop instead of jumping out at the end of the file.

Unless Im missing something (which I probably am) then there isnt anything wrong, but who knows, I missed lecture last week because i wasn feeling well.

Code:
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	int NumIn;
	int NumOut;

	ifstream infile("C:\in.txt", ios::in);
	ofstream outfile("C:\out.txt", ios::out);

	if (infile.eof())
	{
		cout << "There are no more data points int eh data source." << endl;
		cout << "The program will now exit" << endl;
	}	
	
	else (!infile.eof())	
	{
		while (!infile.fail())
		{
			infile >> NumIn;
			outfile << (NumIn + 10) << endl;
			cout << (NumIn + 10) << endl;
		}	
	}	

	infile.close();
	outfile.close();
	
	system("PAUSE");
}
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread

System: Black Dragon
CPU
AMD Phenom II 955BE @ 3.7GHz
Motherboard
DFI Lanparty DK-790FXB-M3H5
Memory
2x2GB G.Skill DDR3 1333
Graphics Card
(2) Sapphire Radeon HD4890 OC
Hard Drive
WD Caviar Black 640GB
Sound Card
Integrated
Power Supply
Corsair HX1000W
Case
Cooler Master HAF932
CPU cooling
Megahalem/OCZ Freeze/2 Green LED 2K RPM CM R4s
OS
Windows Vista x64
Monitor
Hanns.G HH221 1080P
NameUnknown is offline   Reply With Quote
Old 4 Weeks Ago   #2 (permalink)
Console Gamer
 
Twinnuke's Avatar
 
amd ati

Join Date: Nov 2006
Posts: 2,746

Rep: 71 Twinnuke is acknowledged by some
Unique Rep: 63
Trader Rating: 1
Default

Is it possible you aren't supposed to have the "" around the file name?

scratch that i just wasnt paying attention, i know where it comes from now.
__________________
Currently Wanting:
Bad Company 2
Mass Effect 2
FFXIII
My new RiG!

Mysterygoogle just sent me "a pterydactyl just touched me inappropiately"

R.I.P. Rocky the Ferret 3/27/08 =(


If you can solve a paradox then it wasn't a paradox in the first place, but rather a very hard question.

System: Jibbing
CPU
AMD Athlon 64 X2 - 6000+ 3.1GHZ
Motherboard
MSI 740G M-ATX
Memory
2GB (2x1GB) OCZ DDR2-800
Graphics Card
VisionTek HD3870 512MB [837/1215]
Hard Drive
Seagate 300GB 7.2k + Desktar 1TB 7.2K
Power Supply
Mad Dog Sure-Power 430 Watt (Blue LED + 120mm Fan)
Case
Rocket Fish
CPU cooling
Xiggy S1283
OS
Vista 64-bit Ultimate
Monitor
19" Gateway Monitor
Twinnuke is offline   Reply With Quote
Old 4 Weeks Ago   #3 (permalink)
AMD Overclocker
 
NameUnknown's Avatar
 
amd ati

Join Date: Apr 2009
Location: Ohio
Posts: 1,060

Rep: 45 NameUnknown is acknowledged by some
Unique Rep: 38
Trader Rating: 1
Default

Ive got quotes around both file paths :\
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread

System: Black Dragon
CPU
AMD Phenom II 955BE @ 3.7GHz
Motherboard
DFI Lanparty DK-790FXB-M3H5
Memory
2x2GB G.Skill DDR3 1333
Graphics Card
(2) Sapphire Radeon HD4890 OC
Hard Drive
WD Caviar Black 640GB
Sound Card
Integrated
Power Supply
Corsair HX1000W
Case
Cooler Master HAF932
CPU cooling
Megahalem/OCZ Freeze/2 Green LED 2K RPM CM R4s
OS
Windows Vista x64
Monitor
Hanns.G HH221 1080P
NameUnknown is offline   Reply With Quote
Old 4 Weeks Ago   #4 (permalink)
Programmer
 
Korben's Avatar
 
intel nvidia

Join Date: Oct 2008
Location: Florida
Posts: 2,403

Rep: 85 Korben is acknowledged by some
Unique Rep: 76
Trader Rating: 13
Default

Try something like this.


Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;


int main() 
{
	const char* filename = "test.txt";
	std::ifstream inFile(filename);

	// Make sure the file stream is good
	if(!inFile) 
	{
		cout << endl << "Failed to open file " << filename;
		return 1;
	}

	int n = 0;
	while(!inFile.eof()) 
	{
		inFile >> n;
		int x = n+10;
		cout << x << endl;
	}
	cout << endl;
	system("pause");
	return 0;
}
edit:

I probably should have just told you what was wrong with yours instead. I had to do this to get it working for your file.

Code:
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	int NumIn;
	int NumOut;

	ifstream infile("c://in.txt", ios::in);//use string literal // to create backslash / 
	ofstream outfile("c://out.txt", ios::out);//^^ vice versa

	if (infile.eof())
	{
		cout << "There are no more data points int eh data source." << endl;
		cout << "The program will now exit" << endl;
	}	
	
	else if (!infile.eof())//cannot have parameters for else needs to be else if
	{
		while (!infile.fail())
		{
			infile >> NumIn;
			outfile << (NumIn + 10) << endl;
			cout << (NumIn + 10) << endl;
		}	
	}	

	infile.close();
	outfile.close();
	
	system("PAUSE");
}
__________________
Currently Playing: Aion
Steam - KForKorben X-Fire - kforkorben
I did Latty's Linux Challenge and I now have tried Linux!
I am 54% addicted to Counterstrike. What about you?
4Ghz

System: Core i7 Build
CPU
i7 920 | 3.8Ghz
Motherboard
Asus | P6T Deluxe
Memory
Corsair | 6GB | 1600Mhz
Graphics Card
2x | EVGA | GTX 260 | SLI
Hard Drive
2x | VR 300GB + 1TB | Raid 0
Sound Card
Asus | Xonar DX
Power Supply
Corsair | 1000HX
Case
Antec 1200 | Window Mod
CPU cooling
TRUE 1366 | 2x Ultra Kaze
GPU cooling
Stock
OS
Windows 7 Ultimate| x64
Monitor
Samsung | T260

Last edited by Korben : 4 Weeks Ago at 04:11 AM
Korben is offline   Reply With Quote
Old 4 Weeks Ago   #5 (permalink)
Security Sleuth
 
Pooping^fish's Avatar
 
intel nvidia

Join Date: Jul 2007
Location: egypt
Posts: 1,267

Rep: 67 Pooping^fish is acknowledged by some
Unique Rep: 62
Trader Rating: 3
Default

You check for EOF before even reading, what the ****?

Also, you need to handle data types. this isnt your little scripting language, its up to you to do things correctly. Cast and/or atoi() values.
Id also suggest manually parsing the file as always, its your best bet. Have a common data format for the program and error check for anything else.

Also, dont use the System() call if you plan on distributing this at all.
__________________
Quote:
"O, hai! Want som pRon? Dwnlod ths kodk frst. Its teh bst pRonz ever, we prmis." -GibbyGano
Proud Member of the Linux Gaming Community
I am your friend.

System: ragequit
CPU
Q9550 4ghz @ 1.25v
Motherboard
Asus Max 2 formula
Memory
OCZ LV blade 1:1 950mhz
Graphics Card
8800gtx 610/1ghz
Hard Drive
7200.10 250gb
Sound Card
X-FI Extreme Music
Power Supply
750w Toughpower
Case
Lian li pc-65
CPU cooling
TRUE
GPU cooling
stock
OS
leetlinucks
Monitor
24" Westy
Pooping^fish is offline   Reply With Quote
Reply


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



All times are GMT -5. The time now is 08:59 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.10927 seconds with 8 queries