-
April 21st, 2012, 11:26 AM
#1
Reading/Writing C++ File Question
Hello,
I am wanting to read a data file that is placed in my project file where the driver.cpp, function.cpp and function.h are located.
I want to the user create an output file name. How can I do this and where should it be placed?
Also I put my other question below some of this code.
Thanks for the help!
Driver.cpp
Code:
// Driver.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Function.h"
using namespace std;
int main ()
{
//vars
int flowData [400];
string Dates [400];
int highIndex, lowIndex;
Header();
int total = ReadFile(Dates, flowData);
bool Write;
////Problems reading or getting file; Closes program
//if (total == -99)
//{
// cout <<"\nTrouble Reading input file, closing program";
// exit(1);
//}
AnalyzeFile(flowData, total, highIndex, lowIndex);
Write = WriteFile();
cout << "\n";
return 0;
}
Function.cpp
Code:
// Function.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Function.h"
using namespace std;
#define FILE "FlowData.csv"
#define FILEOUT "FlowDataResults.txt"
void Header ()
{
cout <<"\nProgram: 7 Part: 2"
<<"\nData from http://www.usbr.gov/lc/region/g4000/NaturalFlow/NaturalFlows1906-2008_withExtensions_1.26.11.xlsx"
<<"\nObject: Web Research & Reading & Writing Data Files\n\n";
}
int ReadFile(string Dates [], int flowData [])
{
ifstream input;
input.open(FILE);
//Checking file and seeing if I can open it
if (!input)
{
cout <<"\nCan't open file, exiting!";
return false;
}
//Reading the file
cout <<"\nReading: " << FILE << endl;
int i = 0;
while (!input.eof() )
{
getline(input, Dates[i], ',');
input.ignore();
++i;
}
//Telling user where the location is
cout <<"Located: Folder" << endl;
cout <<"Written: Yes";
cout <<"\n\nGood Bye";
input.close();
return i;
}
void AnalyzeFile(int flowData [], int total, int &rHigh, int &rLow)
{
rHigh = rLow = 0;
int highFlow, lowFlow;
highFlow = lowFlow = flowData[0];
for(int i = 1; i < total; ++i)
{
//Seeing the low flow
if (flowData[i] < lowFlow)
{
lowFlow = flowData[i];
rLow = i;
}
//Seeing the high flow
if (flowData [i] > highFlow)
{
highFlow = flowData [i];
rHigh = i;
}
//Displays the results with high and low flows along with the acre feet per month
//Need to put this somewhere so it can print the results
cout << "\nThe highest flow was " << flowData[rHigh];
cout << "\nAcre-feet per month in " << flowData[rHigh] << endl;
cout << "\nLowest flow was " << flowData[rLow];
cout << "\nAcre-feed per month in " << flowData[rLow] << endl;
}
}
bool WriteFile()
{
//Asking user for file name
ofstream output;
output.open(FILEOUT);
//Checking to see if I can open the results text file
if (!output)
{
cout <<"\nFile cannot open!";
return false;
}
//printing the results in a text file .txt
output <<"C++ Data Created By: "
<<"\nThanks for using the C++ Data Reader";
output.close();
return true;
}
I'm also wanting to know where I should put these two lines at.
Code:
//Displays the results with high and low flows along with the acre feet per month
//Need to put this somewhere so it can print the results
cout << "\nThe highest flow was " << flowData[rHigh];
cout << "\nAcre-feet per month in " << flowData[rHigh] << endl;
cout << "\nLowest flow was " << flowData[rLow];
cout << "\nAcre-feed per month in " << flowData[rLow] << endl;
I don't want to show the results to the user in the cmd, but want to show the results in a text file the user created.
Function.h
Code:
// Function.h
#include <string>
using namespace std;
void Header();
int ReadFile(string Dates [], int flowData []);
void AnalyzeFile(int flowData [], int total, int &rHigh, int &rLow);
bool WriteFile();
-
April 22nd, 2012, 10:20 AM
#2
Re: Reading/Writing C++ File Question
-
April 22nd, 2012, 10:34 AM
#3
Re: Reading/Writing C++ File Question
Have you tried running this in the debugger? If not do that, learning how to debug is as important as learning the language syntax.
Single step each line (stepping into the funtions) and check if the code does what you want it to do. I think it will be real aha-experience for you.
-
April 22nd, 2012, 10:35 AM
#4
Re: Reading/Writing C++ File Question
Hi,
Debugging what? I have nothing to debug as of yet. Plus that's not what I'm asking.
-
April 22nd, 2012, 11:17 AM
#5
Re: Reading/Writing C++ File Question
You chose wrong forum for your question.
It is Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.while your question has nothing to do with Visual C++ nor with Windows GUI itself.
As for your question: you should implement it somewhere before you'd try to read from the file the user had chosen.
Victor Nijegorodov
-
April 22nd, 2012, 11:53 AM
#6
Re: Reading/Writing C++ File Question
Originally Posted by VictorN
Ah, I'll just go somewhere else for help. Since really none here wants too. Don't have time to play the around game.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|