So I am having trouble writing a code that opens a file that I have saved on my computer. I need to access a file, open it, read it, and then close it.
this is what I have so far.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
string Randomtxt;
int value;
int numbers;
int even;
int odd;
double sum;
double average;
cout << "Butterfly Girl we need your help! What is the file name?" << endl;
cin >> Randomtxt;
ifstream inputFile(Randomtxt);
if(Randomtxt)
{
cout << "Success!" << endl;
while (Randomtxt >> value)
{
cout << value << endl;
numbers++;
sum += numbers;
if(value % 2 ==0)
even++;
else
odd++;
}
}
else
{
cout << "There is an error opening the file." << endl;
}
if (numbers > 0)
average = sum / numbers;
else
average = 0.0;
cout << "There are " << numbers << "in the file" << endl;
cout << "The even numbers are " << even << endl;
cout << "The odd numbers are " << odd << endl;
cout << "The sum is " << sum << endl;
cout << "The average of all the numbers is " << average << endl;
Randomtxt.close();
return 0;
}
Start with placing the file in the same folder as the executable and running the executable from that folder (not from within your programming environment. Then if you type the name of the file correct, it should find it.
If you want to run your program from within your programming environment, place a copy of the file in the folder that contains your project file.
When in doubt, place the folder in a root directory, like C:\filename.txt.
When you enter the file name you should enter the full name, like "C:\filename.txt", if the file is not in the same folder.
On windows you may also have to use escape characters, so your would type "C:\filename.txt" like "C:\\filename.txt"
p.s. For your future posts, please place your code inside the code-tags (use the "<>"-button)
if(Randomtxt)
You're testing the filename, not the inputFile.
while (Randomtxt >> value)
You're trying to read from the string, not inputFile.
Randomtxt.close();
You're trying to close the string, not inputFile.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
usingnamespace std;
int main ()
{
string Randomtxt;
int value;
int numbers;
int even;
int odd;
double sum;
double average;
cout << "Butterfly Girl we need your help! What is the file name?" << endl;
cin >> Randomtxt;
std::ifstream ifs ("Randomtxt", std::ifstream::in);
if(ifs.is_open())
{
cout << "Success!" << endl;
while (ifs >> value)
{
cout << value << endl;
numbers++;
sum += numbers;
if(value % 2 ==0)
even++;
else
odd++;
}
}
else
{
cout << "There is an error opening the file." << endl;
}
if (numbers > 0)
average = sum / numbers;
else
average = 0.0;
cout << "There are " << numbers << "in the file" << endl;
cout << "The even numbers are " << even << endl;
cout << "The odd numbers are " << odd << endl;
cout << "The sum is " << sum << endl;
cout << "The average of all the numbers is " << average << endl;
ifs.close();
return 0;
}