-
Input File Error
Hello Everybody,
I've been trying to figure out why my code will not read from my file for the past 7 hours - I kid you not. I have a new found respect for the people that do understand the problem and exetreme appreciation to the individual(s) who can guide me in correcting the errors. I have about 3 weeks of Bloodshed experience, so I maybe slow on the uptake.
Thank you
I keep getting "Error opening, CPP3_1, file" - I've tried a variety of "C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1.txt" too
Text on file: 150
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
#define FLUSH cin.clear(); while(cin.get()!='\n');
int main( )
{
ifstream InFile;
int numIn;
cout << fixed << showpoint << setprecision(2);
InFile.open("C:\\CPP3_1.txt");
if(!InFile)
{
cerr << "\nError opening, CPP3_1, file." << endl;
}
//else cout << "\nOpened file.\n";
cout << "\nReading a text file.\n";
while ( InFile >> numIn )
{
cout << numIn << " \n";
}
cout << "I hate this" << endl;
InFile.close();
cout << endl;
cin.get();
system("pause");
return 0;
}
-
Well, you need to know where the file is in order to open it. Just guessing random directories is probably a bad idea.
-
change your numIn type to be chat since your reading char here
Code:
while ( InFile >> numIn )
{
cout << numIn << " \n";
}
And then try readin ti again. Perhaps you should also open the file for reading by specifing it in the opne function call.
Code:
InFile.open("C:\\YServer.txt", ios::in );
ssharish
-
I'm lost and I'm not trying guess.
The title of my text file is "CCP3_1", the only thing on the file is the number "150". This is the address on my computer "C:\Users\nathan\Desktop\C\MY Work\CPP3_1". I've tried a myriad of slightly modified addresses since the original doesn't read.
-
You're trying to open C:\CPP3_1.txt, but then you say the file is elsewhere, and you've tried many variations. That sounds like guessing to me. Get with it. Do you even know what you're opening? Sounds like your file doesn't even have an extension if what you posted is accurate.
-
Macgyver - What do you mean "get with it"? Look man - I don't have as much C++ knowledge as you do - I'm trying to use the knowledge and resources I have to solve my problem. If you don't want to help, so be it.
Here is how the corrected code reads now - it still will not open:
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main( )
{
ifstream InFile;
char numIn;
cout << fixed << showpoint << setprecision(2);
InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1", ios::in );
if(!InFile)
{
cerr << "\nError opening, Crappy, file." << endl;
}
//else cout << "\nOpened file.\n";
cout << "\nReading a text file.\n";
while ( InFile >> numIn )
{
cout << numIn << " ";
}
InFile.close();
cout << endl;
system("pause");
return 0;
}
-
Maybe because "My work" has a space? Try it, though its just a wild guess
If it has .txt extension include it on the above code
-
Spaces are irrelevant.
Place the file in C:\
Then try
InFile.open("C:\\CPP3_1.txt");
Or
InFile.open("C:\\CPP3_1");
One of them should work.
-
I tried - it didn't work - thank you
-
Elysia - by moving it directly into C:\ - it worked!!! You are an extremely awesome person and I can't thank you enough!!!!
-
Then your path to the file is incorrect.
You can use that to continue troubleshooting if you wish.
-
Elysia - you are right. The path was incorrect because I had named the folder and text file the same thing - I just never caught on to it.
Wrong:
Code:
InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1", ios::in );
Right:
Code:
InFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP3_1\\CPP3_1.txt");
Thank you again!