|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
C++ issues reading from files
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | ||||||||||||
|
AMD Overclocker
![]() |
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
|
||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||
|
Console Gamer
![]() |
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.
|
|||||||||||
|
|
|
|
|
#3 (permalink) | ||||||||||||
|
AMD Overclocker
![]() |
Ive got quotes around both file paths :\
__________________
955BE @ 3.7 \ \ \ Possibly Epic Thread \ \ \ AMD Dragon \ \ \ bow before your master....meticadpa \ \ \ Epic Thread
|
||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Programmer
![]() |
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;
}
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");
}
__________________
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
Last edited by Korben : 4 Weeks Ago at 04:11 AM |
|||||||||||||
|
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Security Sleuth
![]() |
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:
Proud Member of the Linux Gaming CommunityI am your friend.
|
||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|