Reading/Writing C++ File Question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Reading/Writing C++ File Question

  1. #1
    Join Date
    Apr 2012
    Posts
    8

    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();

  2. #2
    Join Date
    Apr 2012
    Posts
    8

    Re: Reading/Writing C++ File Question

    Bump

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,639

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  4. #4
    Join Date
    Apr 2012
    Posts
    8

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,545

    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

  6. #6
    Join Date
    Apr 2012
    Posts
    8

    Re: Reading/Writing C++ File Question

    Quote Originally Posted by VictorN View Post
    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.
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center