i/o stuff

This is a discussion on i/o stuff within the C++ Programming forums, part of the General Programming Boards category; I don't understand the part magos posted. Which of course is the majority of it. I would appreciate you explaining ...

  1. #16
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I don't understand the part magos posted. Which of course is the majority of it. I would appreciate you explaining it.
    Last edited by cerin; 02-18-2005 at 06:06 PM.
    My computer is awesome.

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    It sort of works now. It displays the file but it also displays USERDOMAIN=RE_ after the document. How do I fix that??
    My computer is awesome.

  3. #18
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    OK, so the code is not that hard really. We will look at it piece by piece.

    1. Create a pointer of type character to hold the file. We will initially set this pointer to NULL.
    Code:
    [COLOR=Bluechar[/COLOR]* myFileBuffer = NULL;
    2. Lets create an integer to hold the determined size of the file:
    Code:
    int myFilesize;
    3. Lets open a file:
    Code:
    myFile.open("myFile.txt", ios_base::in);
    4. This is where it gets a little confusing for some people. We need to determine the size of the file so we know how much to read. In order to do
    this we are going to take advantage of two members of fstream, seekg and tellg.

    a. seekg places the current position in the stream. So if we did something like this:
    Code:
    myFile.seekg(0, ios_base::end);
    we would move 0 char away from the end of the file. Similarly if we did something like this:
    Code:
    myFile.seekg(2, ios_base::beg);
    We would move 2 char away from the begginning of the file, aka we would skip the first two characters.

    b. tellg() is even simpiler. It returns the current position of the stream pointer. Thus if we did this:
    Code:
    myFile.seekg(2, ios_base::beg);
    cout << myFile.tellg();
    We would recieve the value of 2. (Numbers start at 0).

    Alright now with that discussed one can see that to determine the length of a file you would
    simply place the stream input at the end of the file and then ask for the number of characters skipped.
    Code:
    myFile.seekg(0, ios_base::end);
    myFilesize = myFile.tellg();
    5. Now that we know how large the file is, we need to allocate enough memory in our buffer to hold the file. We accomplish this by using new:
    Code:
    myFileBuffer = new char[myFilesize];
    6. With the memory allocated now it is time to read the file into our buffer. However remember that
    we moved the file pointer to the end of the file, so the first thing we need to do is move it back to the begginning of the file:
    Code:
    myFile.seekg(0, ios_base::beg);
    7. Now we read the file into our buffer:
    Code:
    myFile.read(myFileBuffer, myFilesize);
    8. Like all good strings we need to ensure that they end with '/0'. So we add it to the end of the string:
    Code:
    myFileBuffer[myFilesize] = '\0';
    9. And finally we print out the contents of our file buffer with cout:
    Code:
    cout << myFileBuffer << endl;
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    It sort of works now. It displays the file but it also displays USERDOMAIN=RE_ after the document. How do I fix that??
    My computer is awesome.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw I/O vs. Stream I/O
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 08:32 AM
  2. quick file i/o
    By breanne in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-12-2002, 03:29 PM
  3. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM
  4. Linked lists and file i/o, and some other stuff
    By ninja in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2002, 07:15 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21