Thread: Memory Issue...?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    If you want to keep it simple, you could do this.

    Have a file named Genisis.txt with this in it

    Code:
    1:1 In the..
    1:2 blah
    I decided to try out a little test about how you would go about allocating memory and such for each book. (You could just load one book, or segments of books at a time)

    Code:
    #include <iostream>
    #include <fstream>
    
    char *fileBuffer;
    
    const long BookSize(char *book)
    {
      std::ifstream tempFile(book);
    	
      if(!tempFile.is_open()) {
        std::cout << "Error opening " << book;
        exit(1);
      }
    	
      tempFile.seekg(0, std::ios::end);
      long fileSize = tempFile.tellg();
      tempFile.close();
    	
      return fileSize;
    }
    
    void ReadBook(char *book, long fileSize)
    {
      std::ifstream file(book);
    	
      if(!file.is_open()) {
        std::cout << "Error opening " << book;
        exit(1);
      }
    	
      file.read(fileBuffer, fileSize);
    	
      file.close();
    }
    
    int main()
    {
      char book[50];
    	
      std::cout << "Type in the name of a Book: ";
      std::cin  >> book;
    	
      const long size = BookSize(book);
    	
      fileBuffer = new char [size];
    
      ReadBook(book, size);
    
      std::cout << std::endl;
      std::cout << fileBuffer;
      
      delete [] fileBuffer;
      return 0;
    }
    note this is just some example code.

    This is what happens

    Code:
    Type in the name of a book: C:\Genisis.txt
    
    
    1:1 In the begining, God created the heaven and the earth.
    1:2 Something about light.
    
    Where I have a text file named Genisis.txt in C:\ that contains

    1:1 In the begining, God created the heaven and the earth.
    1:2 Something about light.
    Last edited by Vicious; 08-09-2004 at 07:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help finding potential Memory issue
    By JoshNCSU22 in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 09:58 AM
  2. What's the difference?
    By Stonehambey in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 10:26 AM
  3. threads and memory issue
    By Anubhav in forum C Programming
    Replies: 6
    Last Post: 07-25-2006, 04:51 AM
  4. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM