File Sizing Index

Getting back off-set values.

Page 1 of 1

2 Replies - 466 Views - Last Post: 20 August 2009 - 09:22 PM Rate Topic: -----

#1 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

File Sizing Index

Posted 20 August 2009 - 04:26 PM

I have a class that holds the dimensions of a file (in chars and lines). I used a vector
to hold an index of the file, each element would hold an integer, the length of characters
on the line the element represents. When I read the elemental values, they seem to be off by a line.

I have checked my algo of scanning the file and cannot find an error in code. But the returns i get upon execution
are simply wrong.

ouput: (The file in question if FileManager.h)
Lines: 89
Characters : 2811
What line would you like to enquire: 1
Line 1 has 30 characters.



FileManager.h
// Start
#ifndef FILEMANAGER_H_INCLUDED
#define FILEMANAGER_H_INCLUDED

#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <string>

using namespace std;

//** FileDimen **//

class FileDimen  // There be dragons here.
{
private:
	// Will store dimensions of file.
	// Each array element will represent a line.
	// And the value of each element will represent the number
	// of characters per line.
	vector<int> FileDimensions;
	// The Total Number Of Lines
	int LinesTotal;
	// the Total number of characters
	int CharactersTotal;
public:
	// Private Data Access //
	// Reading
	// Say number of lines.
	int SayLines(void);
	// Say number of characters on line <arg1>
	int SayChars(unsigned int arg1);
	// Say CharactersTotal
	int SayCharsTotal(void);
	// Say FilePath
	string SayFilePath(void);

	// Setting
	// Set FilePath to <arg1>
	void SetFilePath(string arg1);

	// Scan File (finaly, the cool code!)
	void ScanFile(string arg1);
};

		// Saying
		int	FileDimen::SayChars(unsigned int arg1){
				if(arg1 > FileDimen::FileDimensions.size()){
				  std::cout << "Fatal: FileDimen::SayChars() exceeds FileDimensions.size()" << std::endl;
				  exit(EXIT_FAILURE);
			   }
			   return FileDimen::FileDimensions.at(arg1);
		}
		int	FileDimen::SayCharsTotal(void){
			   return FileDimen::CharactersTotal;
		}
		int	FileDimen::SayLines(void){
			   return FileDimen::LinesTotal;
		}
		// Utility
		void   FileDimen::ScanFile(string arg1){
			   FileDimen::FileDimensions.clear(); // Clear FileDimensions for new scan.
			   fstream MyTmpFlStream;
			   MyTmpFlStream.open(arg1.c_str(), ios_base::in);
				if(! MyTmpFlStream.is_open()){
					cout << "Fatal: FileDimen could not find file to scan!" << endl;
					exit(EXIT_FAILURE);
				}
					string tmpstr;
					int tmpLine = 0;
					int tmpTotChar = 0;
					while(getline(MyTmpFlStream, tmpstr)){
					  tmpLine++;
					  tmpTotChar = (tmpTotChar + tmpstr.length());
					  FileDimen::FileDimensions.push_back(tmpstr.length()); // I have concerns, i am returning a size_t as an argument for
																			// to vector.push_back(). But my vector is using type int!
					}
						FileDimen::CharactersTotal =  tmpTotChar;
						FileDimen::LinesTotal = tmpLine;
				MyTmpFlStream.close();
		}

#endif // FILEMANAGER_H_INCLUDED
// END



tstmain.cpp:
#include "FileManager.h"
#include <iostream>

using namespace std;

int main(int argc, char * argv[])
{
	if (argc < 1)
	{
		cout << "Too few arguments!!!!" << endl;
		cin.get();
		return 1;
	}
	unsigned int a;
	FileDimen MyFileDimenTst;
	string tmpString = argv[1];
	MyFileDimenTst.ScanFile(tmpString);
	cout << "Lines: " << MyFileDimenTst.SayLines() << endl;
	cout << "Characters : " << MyFileDimenTst.SayCharsTotal() << endl;
	cout << "What line would you like to enquire: ";
	cin  >> a;
	cout << "Line " << a << " has " << MyFileDimenTst.SayChars(a) << " characters. " << endl;
	cin.get();
	return 1;

}



It seems to miss the entire first line!
Thanks all.

Is This A Good Question/Topic? 0
  • +

Replies To: File Sizing Index

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: File Sizing Index

Posted 20 August 2009 - 06:18 PM

Are vectors 0-based, or 1-based?
Was This Post Helpful? 0
  • +
  • -

#3 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Re: File Sizing Index

Posted 20 August 2009 - 09:22 PM

EDIT: Wait is that a rhetorical question that is suppose to help Delta_Echo?

My bad.

This post has been edited by Mrafcho001: 20 August 2009 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1