Thread: Savign the position of a pointer in an input file

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Savign the position of a pointer in an input file

    Hey,

    I'm working with a binary input file. a bunch of functions open it an close it once they are done. I want to have one function open it read, say half of it, and save the position of the pointer there. So when the next function opens the file it can start reading from where the last one left off.

    I really have no idea of how to do this. Any ideas of where I can find the info?

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    If you're reading tellg() returns the index of the file pointer. If you're writing, use tellp(). The return values are of type streampos (think of it as being equivalent to a long).

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, I understand how that works now. But I have been using C i/o until now and now I have to change all of it. and I'm not readlly sure how to output to a file in C++. I have an ofstream object for the putput file. Can I use say out.putg<< var_name;

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, those seem to work, but I have problems with input output now. I'm trying to use it to fill a matrix and print it out. It prints uselessness.
    Here's the code:
    Code:
    # include<iostream>
    #include <cstdio>
    #include <fstream>
    
    int
    main ()
    {
    	float **mtxVicVals;
    
    	std::ofstream out;
    	out.open("J:\\EICAS Simulator\\Engine Files\\test.txt");
    
    	mtxVicVals = new  float*[10];
    
    	for(int i=0; i<10; i++)
    		mtxVicVals[i] = new float[3];
    
    	for (i=0; i<10; i++)
    	{
    		for (int j=0; j<3;j++)
    			mtxVicVals[i][j] = j;
    	}
    
    	for (i=0; i<10; i++)
    	{
    		for (int j=0; j<3;j++)
    		{
    			out.put ( mtxVicVals[i][j]);
    			out<<"\t";
    			
    		}
    		out<< "\n";
    	}
    
    	return 0;	
    	
    }
    and the output is:
    Code:
     			
     			
     			
     			
     			
     			
     			
     			
     			
     		

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I fixed with
    Code:
    out << mtxVicVals[i][j];
    seems to work ok.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    change
    out.put ( mtxVicVals[i][j]);
    to
    out << mtxVicVals[i][j];

    [edit]
    Nevermind.

    ostream::put() takes a char as it's argument and you're providing a float. That's the reason for the unexpected results.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Now, I'm trying to change it to read in a binary file with inp>>var_name. How do I specify how many bytes to read? Like in C I use for example:
    Code:
    fread(&DataFormat,sizeof(long),1,inp);
    what is it with C++ binary reading?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You could use the ios::binary mode and then use the usual stream methods/operators or you could use the istream read() method, though there are pros and cons with the latter.
    You're only born perfect.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    yeh, I opened the file in binary mode, but with a call like inp>>DataFormat; how do I tell it how many bytes to read?

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The same way you tell >> how many char to read when using text mode and reading into an int----you leave it up to the compiler/program to do it for you, you don't have to worry about it.
    You're only born perfect.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    Code:
    ifstream stream("blah", ios::binary);
    
    //Let's say you want to read 10 bytes from the file
    char buffer[10];
    stream.read(buffer, 10);
    
    //And you want to save your position
    //streamoff is the type used for stream offsets
    streamoff pos = stream.tellg();
    stream.close();
    stream.open("blah", ios::binary);
    stream.seekg(pos);
    //Now you're ready to read again

  12. #12
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I think I understand how it's all done now, but my code disgrees with me completely. I have to pass the file pointers to other functions and they don't like t. Here's one example:
    function declaration:
    Code:
    void SkipWS(ifstream inp);
    the function:
    Code:
    void SkipWS(ifstream inp)  // Skips white space and comments
    {
    	int ch;
    
    	do
    	{
    		inp>>ch;
    		
    		if (ch == '%')  // Comment starts, read it to the end of line
    		{
    			do
    			inp>>ch;
    			while( ch != '\n' && ch != EOF);
    		}
    			
    		else if (ch == '\n') // do not exit the loop at New Line
    			continue;
    	}
    	
    	while (isspace(ch) && ch != EOF);
    
    
    
    	fseek(inp,-1L,1);  // Pointer is left at the end of white space or comment,
    	                   //  at the next char to be read
    }
    Aside from the fseek() error I get thing slike this:
    Code:
     'ifstream' : undeclared identifier
    h:\gui\initial gui\bafparse.h(52) : error C2146: syntax error : missing ')' before identifier 'ptr'
    'ifstream' : ambiguous symbol
    h:\gui\initial gui\edfparse3.h(52) : error C2146: syntax error : missing ')' before identifier 'inp'
    h:\gui\initial gui\edfparse3.h(52) : error C2182: 'SkipWS' : illegal use of type 'void'
    h:\gui\initial gui\edfparse3.h(52) : error C2059: syntax error : ')'
    and so on, 87 errors. I did include fstream in the header file before declaring the functions.

  13. #13
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I think I understand how it's all done now, but my code disgrees with me completely. I have to pass the file pointers to other functions and they don't like t. Here's one example:
    function declaration:
    Code:
    void SkipWS(ifstream inp);
    the function:
    Code:
    void SkipWS(ifstream inp)  // Skips white space and comments
    {
    	int ch;
    
    	do
    	{
    		inp>>ch;
    		
    		if (ch == '%')  // Comment starts, read it to the end of line
    		{
    			do
    			inp>>ch;
    			while( ch != '\n' && ch != EOF);
    		}
    			
    		else if (ch == '\n') // do not exit the loop at New Line
    			continue;
    	}
    	
    	while (isspace(ch) && ch != EOF);
    
    
    
    	inp.seekg(-1,ios::end);  // Pointer is left at the end of white space or comment,
    	                   //  at the next char to be read
    }
    I get things like this:
    Code:
     'ifstream' : undeclared identifier
    h:\gui\initial gui\bafparse.h(52) : error C2146: syntax error : missing ')' before identifier 'ptr'
    'ifstream' : ambiguous symbol
    h:\gui\initial gui\edfparse3.h(52) : error C2146: syntax error : missing ')' before identifier 'inp'
    h:\gui\initial gui\edfparse3.h(52) : error C2182: 'SkipWS' : illegal use of type 'void'
    h:\gui\initial gui\edfparse3.h(52) : error C2059: syntax error : ')'
    and so on, 87 errors. I did include fstream in the header file before declaring the functions.
    And the declaration
    Code:
    	streamoff FilePos;
    gives an error
    Code:
    error C2501: 'streamoff' : missing storage-class or type specifiers
    and there is a variety more of errors. I don't think it undestands the # include <fstream>

  14. #14
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    In addition to #include'ing <fstream> you have to keep in mind that ifstream, streampos, etc. are all encapsulated in namespace std, so you either have to specify scope somehow (with a "using" statement or the scope resolution operator).

    "streamoff" is not a type. I believe you mean to use "streampos".

    A fatal flaw in your function is that you are attempting to read spaces with the extraction operator ">>" but this operator skips whitespace (space, tabs, carriage returns, and linefeesd). Read up on C++ file IO a bit for more details. Try google.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 04-21-2005, 10:59 AM