3 Replies - 537 Views - Last Post: 25 June 2012 - 08:55 AM Rate Topic: -----

#1 johnny2310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-June 12

To output list of date, difference btw max and min value

Posted 23 June 2012 - 03:37 AM

Hi, I am a beginner and have some problems with my C++ code. My task is to open the mainfile MainFile.txt with a list of 31 subfiles named file1.csv, file2.csv and etc. I need to then open the subfile and get the date and difference between max value and min value. Finally, I will output each result from each subfile and store them in an output file named result.csv.

I am only able to get the output result of the file1.csv. But unable to get the results of file2.csv, file3.csv and etc. Can anyone assist me with that? No errors were detected and I am able to store my file1.csv result into result.csv.
Thank you very much.

MainFile.txt
file1.csv
file2.csv
file3.csv
.......
file31.csv

file1.csv with format (dd/mm/yyyy);value
01/01/2011;01:00;0.090
01/01/2011;02:00;0.100
....... (many many lines up to 100+ with each subfile diff no. of lines)
01/01/2011;10:00;0.200

result.csv with format (mm/dd/yyyy);diff
01/01/2011;0.11
01/02/2011;0.32
........
01/31/2011;0.33


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "myVector.h"
#include "Date.h"
using namespace std;

int main()
{
	Date D;
        string fileName;	
	double units;
	double maxUnits;				
	double minUnits;		
	double diffUnits;				

	myVector<double> vecList(50);		//to delcare vecList of type double
	myVector<double> copyVecList1(50);     //to delcare copyVecList1 of type double

	cout << "Please enter the input file name " << endl;
	cin >> fileName;

	ifstream inFile;			//To declare input as inFile
	inFile.open(fileName.c_str());		//To open the fileName
	ofstream outFile;			//To declare output as outFile

        if (!inFile)
	{
         cout << "File does not exists. Program terminates." << endl;
         system("PAUSE");
         return 1;
	}
	 else	
        {
	outFile.open(string("Result" + ".csv").c_str());
	
	string subfile;	        //To declare string subfile
	ifstream readFile;	//To declare input as readFile

	if(inFile.is_open())	//If fileListAug.txt is open
	{
          while (!inFile.eof())		//While file is not end of file
	  {
	    inFile >> subfile;
	    readFile.open(subfile);	//Open subfile		
	    if (readFile.is_open())	//If subfileis open
	    {
	      readFile>> D;             //Get date with format month/day/year
	      readFile >> minUnits;	//Get the first unit stored as variable minUnits

	      while (!readFile.eof())	//While subfile is not end of file
	      {
		readFile >> units;	//Get units
	      }

	vecList.initializeVec();	//Call initializeVec()
	vecList.pushes(units);		//Push the units
	copyVecList1 = vecList;		//Copy vecList into copyVecList1
							
	maxUnits = units;		//To find the max units
	if (units > maxUnits)
	{
         if (!copyVecList1.isFullVec())
	 copyVecList1.pushes(units);
	 maxUnits = units;
	}
         else if (units == maxUnits)
	 if (!copyVecList1.isFullVec())
	 copyVecList1.pushes(units);
	 else
	 {
           cout << "Vector overflows. Program terminates." << endl;
           system("PAUSE");
	   return 1;
	 }

         diffUnits = maxUnits - minUnits;
	 cout << D << ";" << diffUnits << endl;			
	 outFile << D << ";" << diffUnits << endl;
	
        }
	else
	 { 
	   cout << "Subfile cannot be found." << endl;
	 }
         
         system("PAUSE");
	 return 0;
       }
     }
    }
   };




Is This A Good Question/Topic? 0
  • +

Replies To: To output list of date, difference btw max and min value

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: To output list of date, difference btw max and min value

Posted 23 June 2012 - 04:58 AM

There's a few files missing, "myVector.h" and "Date.h" so I haven't tried compiling it myself, but I'll have a guess. You don't seem to close the subfiles after reading them in, so maybe the loop is failing, because readFile is already associated to an open stream. At the end of the subfile reading loop you'll want to call readFile.close()

Also, it may have just copied across funky but the indentation isn't consistent and makes it quite hard to interpret!
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: To output list of date, difference btw max and min value

Posted 23 June 2012 - 07:08 AM

I see a few problems and have a couple of questions. First what is the purpose of the following line?
outFile.open(string("Result" + ".csv").c_str());

You are dealing with two constants, why are you trying to cast them to a string then use the c_str() function. Why not just use:
outFile.open("Result.csv");


Next the following line:
         while (!inFile.eof())		//While file is not end of file


You should not be using eof to control your data entry loop, this will usually lead to having at least one extra data item. You may want to read this link: Things to Avoid in C/C++.

Next in the following snippet:
         while (!inFile.eof())		//While file is not end of file
         {
            inFile >> subfile;
            readFile.open(subfile);	//Open subfile
            if (readFile.is_open())	//If subfileis open
            {
               readFile>> D;             //Get date with format month/day/year
               readFile >> minUnits;	//Get the first unit stored as variable minUnits

               while (!readFile.eof())	//While subfile is not end of file


You are trying to reuse your readFile stream to read multiple files, which if care is taken can work wonderfully. However you have not taken the proper care. First you should close the currently open file before you try to reuse this stream. And since you are reading until the end of file your stream will be in an error state, which must be cleared before any further stream access can be accomplished. Also, when testing that the file opened correctly you really should test all the error flags, not just the is_open flag. Try:
if(readFile)

The above will test the complete condition of your stream testing all error flags.

But in the case of this code the above problem is not the first cause of your problem which is that the first loop will only run once. You need to find an indentation style you like and use it consistently. Once you find and use an indentation style you should be able to easily spot why this loop will only run once.

There may be other problems, but without a complete compilable program it is very difficult to tell.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 johnny2310  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-June 12

Re: To output list of date, difference btw max and min value

Posted 25 June 2012 - 08:55 AM

Hi Guys, sorry for the messy indentations. I don't mean to make it difficult for all to read. And thanks for the comments that you all have given me. I have figured it out and revised all the things that needs to be changed. I have also included for loop in my program and everything works fine!

Thanks once again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1