GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Apr-2009, 20:43
porsche911nfs porsche911nfs is offline
New Member
 
Join Date: Apr 2009
Posts: 1
porsche911nfs is on a distinguished road

Accessing .dat files within a folder


Below, I have c/p my code. The program is supposed to input a folder name, and inside that folder there are two .dat files with an array of data in each that I am supposed to read and output the average and the maximum values in each. Can someone help me out please? Also, I do not know what to include in my arguments in my first three function definitions...because when I uncomment my function calls, I get errors that refer to those function arguments. Please help ASAP! Thank you in advance for all the help!

CPP / C++ / C Code:
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;
typedef vector<string> stringVector;  //Defines a vector of string values
const int MAX_SIZE = 31;

void OpenInputFile(ifstream& in_file, char string);
void OpenOutputFile(ofstream& out_file, char string);
int ReadArray(ifstream& in_file, int number[]);
float CalcAverage(int array[]);
float CalcMax(int array[]);
int getDir(string dir, stringVector &files);

int main()
{
    ifstream indiv_file;
    ofstream out_file;
    int count = 0, array[MAX_SIZE], next; 
    float average_temp, maximum_temp;
    int start;
    string dir, intro, compound;
    
    intro = "C://*/*/*/*/*/*/*/";
    cout << "Enter directory with files available for use: ";  //Lists the files in the current directory you could input . 
    cin >> dir;
    compound = intro + dir;
    stringVector files = stringVector();
	
    /*cin >> InputFiles/; 
    string dir; 
    get_Dir() --> files[size] 
    ifstream ifs; 
    ifs.open("path+numbers.dat") 
    open((dir+files[i]).c_str())*/
    
    getDir (dir, files);
    
    for (unsigned int count = 0; count < files.size(); count++)
    {
        if (files[count].at(0)!= '.') // if the file is a valid file
        {
           //indiv_file.open((compound + files[i]).c_str());
           indiv_file.open((dir + files[count]).c_str());
           cout << endl << dir + " " + files[count] << endl;
           for (int j = 0; j < count; j++)
	       {
               while (indiv_file >> next)
	           {
                     cout << array[j];
                     j++;
               }
               cout << array[j] << " " << endl;
            }
            if(count == MAX_SIZE)
                 break;
        }
        indiv_file.close();
     }
    //OpenInputFile (array, string);
    //OpenOutputFile (out_file, string);
    //ReadArray (in_file, number[]);
    average_temp = CalcAverage(array);
    maximum_temp = CalcMax(array); 
    
    cin >> start;
    return 0;
}
int getDir (string dir, stringVector &files)
{
	DIR *dp;
	struct dirent *dirp;
	
	if((dp  = opendir(dir.c_str())) == NULL) {
		cout << "Error(" << errno << ") opening " << dir << endl;
		return errno;
	}
	while ((dirp = readdir(dp)) != NULL)  {
		files.push_back(string(dirp->d_name));
	}
	closedir(dp);
	return 0;
}
void OpenInputFile(ifstream &in_file, int string)
{
     char symbol;
     ifstream in;
     in.open("January.dat");
     if (in.fail())
     {
        cout << "Failed to open file." << endl;
        exit(1);
     }
     
     do
     {
        cin.get(symbol);
        cout << symbol;
     } while (symbol != '.');
             
     in.open("February.dat");
     if (in.fail())
     {
        cout << "Failed to open file." << endl;
        exit(1);
     }
}
void OpenOutputFile(ofstream &out_file, int string)
{
     ofstream out;
     out.open("MaxOfArrays.txt");
     out_file.open("AveragesOfArrays.txt");
}
int ReadArray(ifstream &in_file, int number[])
{
     int next;
     ifstream in;
     while (in >> next)
     {
           cout << next;  
     }   
     return 0;
}
float CalcAverage(int array[])
{
     int average, total = 0, i = 0;
     
     for (i; i < MAX_SIZE; i++)
     {
         total += array[i];         
     } 
     
     average = (total / i);
     return 0;  
}
float CalcMax(int array[])
{
     return 0;  
}
Last edited by admin : 03-Apr-2009 at 01:22. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 05-Apr-2009, 13:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Accessing .dat files within a folder


Quote:
Originally Posted by porsche911nfs
... my code...help

Make the program tell you exactly what it is working with at each step.. I think you should never assume that files have been opened properly, so trap error conditions and give the user some information. Maybe you want to let the program attempt to recover (by letting the user try again with other input), or maybe you just want to abort the program.

For example

CPP / C++ / C Code:
.
.
.
    cout << "Enter directory with files available for use: ";
    cin >> dir;
    stringVector files = stringVector();

    cout << "Calling getDir with dir = --->" << dir << "<---" << endl;
    getDir(dir, files);
    cout << "From getDir, the number of files =  " << files.size() << endl;
         

    for (unsigned int count = 0; count < files.size(); count++) {
        if (files[count].at(0) != '.')  // if the file is a valid file
        {
            cout << "Count = " << count
                 << ", attempting to open file --->"
                 << dir + files[count]
                 << "<--- for reading." << endl;
            indiv_file.open((dir + files[count]).c_str());
            if (!indiv_file.is_open()) {
                cerr << "Couldn't open the file." << endl;
                return 0;
            }
.
.
.

Note that when printing debugging information, I usually put some kind of bracket around the strings so that I can see if there are any leading or trailing spaces. So the output from my debugging statement might look like:

Code:
Calling getDir with dir = --->testdir<--- . . . Count = 3, attempting to open file --->whatever<--- for reading.


Quote:
Originally Posted by porsche911nfs
Also, I do not know what to include in my arguments...

Well, first of all, give the specification of what the function is supposed to do. List the arguments and their meaning and describe the return value (if the return type isn't void). Do this (and show us what you did) for whatever function you are having problems understanding, and maybe someone can help.

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy all files from one folder to other darshan.yadav C++ Forum 5 29-Apr-2008 05:17
Reading multiple files in a folder sitha C++ Forum 4 13-Jun-2007 05:25
C++ for moving files across network? dlp C++ Forum 1 25-May-2006 13:22
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 18:23
Using .dat files SpudNuts C++ Forum 3 13-Mar-2005 20:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.