Thread: Accessing .dat files within a folder

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    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!

    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;  
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You should be passing the name of the directory, which is a string, so it seems the signature of OpenInputFile() should be
    Code:
    void OpenInputFile(ifstream& in_file, string dir_name)
    Similarly for OpenOutputFile().

    Also, your call of ReadArray() gives the first argument as in_file, but in main() you have no variable by that name.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    thanks, that makes sense i s'pose. However, I'm getting large numbers (nothing like what it should be - the data in the .dat file is what I should be getting) that don't make any sense. It happens in this portion of the code:

    Code:
    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();
         }
    Can you help me there? Thanks again for the help!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You're printing values from array, but you haven't assigned any values to array's elements so what you're getting is just initialized garbage.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    how do i do that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accessing files over wireless network
    By BobMcGee123 in forum Tech Board
    Replies: 4
    Last Post: 07-29-2006, 02:25 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Files not updating in folder
    By BobS0327 in forum Tech Board
    Replies: 4
    Last Post: 06-06-2005, 05:55 PM
  4. JetAudio won't play .dat files
    By LogicError in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-25-2005, 08:35 AM
  5. Code to display files and sizes of any folder
    By CrackDown in forum Linux Programming
    Replies: 0
    Last Post: 04-22-2003, 11:23 AM