Techno Viking's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
20 (0.02 per day)
Joined:
08-August 10
Profile Views:
459
Last Active:
User is offline Private
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   Techno Viking has not set their status

Posts I've Made

  1. In Topic: EOF and reading text files (C++)

    Posted 13 Oct 2011

    This is GOLD for beginners like me!! THANKS!!! :bananaman:
  2. In Topic: seekg does not work after while

    Posted 13 Oct 2011

    View Postjimblumberg, on 13 October 2011 - 06:14 AM, said:

    Quote

    I am not sure what you mean. After I give it a size of 1, it should re-size to what the space requirement is.


    No it will not automatically re-size. For read() you must pass a pointer to an allocated block of memory where the content read will be stored. And the size in characters of the block of data to be read. The size must be equal to or less than the size of the allocated block of memory where the content will be stored. This buffer does not even need to be dynamic.

    The following snippet shows using a statically allocated array being used by read().
    char buffer[100];
    myfile.read(buffer,100);
    


    However you should not even be using the read() function to read your data file. You should be using std::strings to hold your data and then use either getline() or possibly the stream extraction operator>> to extract the information from the file.


    Jim


    Thanks Jim!
  3. In Topic: seekg does not work after while

    Posted 13 Oct 2011

    View PostJackOfAllTrades, on 13 October 2011 - 04:12 AM, said:



    That would have saved so much headache and agony :( but I was searching for seekg thinking that was the problem initially. Thanks for the link!!!
  4. In Topic: seekg does not work after while

    Posted 12 Oct 2011

    View Postjimblumberg, on 12 October 2011 - 06:30 PM, said:

    Why are you using a char* instead of a std::string? A C-string, which is a null terminated character string, has a fixed size, it will not automatically expand.


    I don't know how to use myfile.read() with a string buffer, it wants it to be char... :(

    View Postjimblumberg, on 12 October 2011 - 06:30 PM, said:

    You are accessing memory that you don't own. You allocate space for one character buffer = new char[1]; then you try to stuff it with 12 characters buffer[11] = 0;.


    I am not sure what you mean. After I give it a size of 1, it should re-size to what the space requirement is. The only problem is that it re-sizes too far or read() is going out of file. Unless I have misunderstood some fundamental law of dynamic arrays.
  5. In Topic: seekg does not work after while

    Posted 12 Oct 2011

    ok, everything works now.. :]

    I understand now that I have not been properly ending the dynamic array, below is what I am talking about.

    text.txt contains "Hello World"

    #include <fstream>
    #include <iostream>
     
    using namespace std;
     
    int main (int argc, char** argv)
    {
    	fstream myFile("test.txt");
    
    	char holdc;
    
    	while(myFile >> holdc)
    	{
    		 
    	}
    
    	myFile.clear();
    	 
        myFile.seekg(0, ios::beg);
     
        char *buffer;			
        buffer = new char[1];
    	 
        myFile.read(buffer, 11);
        
        buffer[11] = 0;	//without this I get junk after my read() input ends	
    
        cout << buffer << endl;
     
    	  
    
        myFile.close();
    }
    
    


    Can you please explain why I have to end the dynamic array like this? I thought it would automatically expand until the input ends? Am I incorrect to say that this behavior is only with the read() function?


    Thanks!

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

Techno Viking hasn't added any friends yet.

Comments

Techno Viking has no profile comments yet. Why not say hello?