ftell() in C++

ftell() in C++

Page 1 of 1

13 Replies - 4561 Views - Last Post: 03 November 2007 - 01:38 AM Rate Topic: -----

#1 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

ftell() in C++

Posted 01 November 2007 - 02:24 PM

Hello,
Firstly do we have ftell() in c++. If yes then why is the code written below giving me this error " cannot convert `char*' to `FILE*' for argument `1' to `long int ftell(FILE*)' ". Pls tell me whats wrong and make corrections to my code.
Thanks,
prads

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;


int main()
{
	
   char rawsignal[38192],position;
 int samplesread,blksize=100;  
 
 
	   fstream file_op("c:\\t1.txt",ios::in);
		while(!file_op.eof())
		{
			  file_op.getline(rawsignal,blksize,'int8');
			  position=ftell(rawsignal);										// not working!!!!!!!   
			  cout<<"Position is"<<position<<endl;
			  samplesread=strlen(rawsignal)-2;
			  cout<<samplesread<<endl;
			  //cout <<rawsignal<<endl;
			 
		}	
	   cout<<rawsignal<<endl;  
		if(samplesread!=blksize)
			  
			   cout<<"Not able to read the specified number of samples  for tracking, exiting!"<<endl;					  
		 file_op.close();
getchar();
	return 0;
	}

This post has been edited by jjhaag: 01 November 2007 - 05:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: ftell() in C++

#2 PuzzleCC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 01-November 07

Re: ftell() in C++

Posted 01 November 2007 - 04:58 PM

to the point of absurdity, to ridiculous lengths

position=file_op.tellp();


:)
you get cursor position in variable character char * ?
Was This Post Helpful? 0
  • +
  • -

#3 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 01 November 2007 - 05:29 PM

View PostPuzzleCC, on 1 Nov, 2007 - 04:58 PM, said:

to the point of absurdity, to ridiculous lengths

position=file_op.tellp();


:)
you get cursor position in variable character char * ?



hello,
I didnot quite understand the last line of ur reply "you get cursor position in variable character char * "...
I did this:
position=file_op.tellp();
but its giving me some wierd looking character.!...Pls help :blink:
Was This Post Helpful? 0
  • +
  • -

#4 PuzzleCC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 01-November 07

Re: ftell() in C++

Posted 01 November 2007 - 06:55 PM

 
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>


using namespace std;


int main()
{

char rawsignal[ 38192 ];
int samplesread, blksize=100;
long position; 


fstream file_op( ".\\t1.txt", ios::in |ios::out );

file_op << "1\n22\n333\n4444" << endl;
file_op.seekp( 0, ios::beg );

while( file_op.getline( rawsignal, blksize ) )
 {
 position=file_op.tellp();
 samplesread = strlen( rawsignal );

 cout << "Line is \"" << rawsignal << "\" End of line position is " << position
	  << " Length of " << samplesread << endl;
 } 

cout << rawsignal << endl; 
if( samplesread != blksize )
	cout << "Not able to read the specified number of samples for tracking, exiting!" << endl; 
file_op.close();
getchar();
return 0;
}


:rolleyes:
Was This Post Helpful? 0
  • +
  • -

#5 PuzzleCC  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 01-November 07

Re: ftell() in C++

Posted 01 November 2007 - 07:39 PM

To pervert
#include <iostream>
#include <stdio.h>


class _file_
 {
public:
	_file_( FILE *file ) { this_file = file; }
	~_file_() {}

	long tell() { return ftell(this_file); };

protected:
	 FILE *this_file;
 };



using namespace std;


int main()
{
FILE *f;
long curpos=0;

f=fopen( ".\\T1.TXT", "rw" );
_file_ ff=f;

fseek( f, 0, SEEK_SET );
cout << ff.tell() << endl;

fseek( f, 0, SEEK_END );
cout << ff.tell() << endl;

fclose( f );

return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 01 November 2007 - 09:30 PM

View PostPuzzleCC, on 1 Nov, 2007 - 07:39 PM, said:

To pervert
#include <iostream>
#include <stdio.h>


class _file_
 {
public:
	_file_( FILE *file ) { this_file = file; }
	~_file_() {}

	long tell() { return ftell(this_file); };

protected:
	 FILE *this_file;
 };



using namespace std;


int main()
{
FILE *f;
long curpos=0;

f=fopen( ".\\T1.TXT", "rw" );
_file_ ff=f;

fseek( f, 0, SEEK_SET );
cout << ff.tell() << endl;

fseek( f, 0, SEEK_END );
cout << ff.tell() << endl;

fclose( f );

return 0;
}



Thanks for ur help but unfortunately both the above programs are not working correctly. I just want to know th position of the pointer in my code. Pls tell me how i should do that.
Thanks,
Prads :D
Was This Post Helpful? 0
  • +
  • -

#7 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: ftell() in C++

Posted 02 November 2007 - 10:42 AM

View Postprads, on 1 Nov, 2007 - 10:30 PM, said:

Thanks for ur help but unfortunately both the above programs are not working correctly. I just want to know th position of the pointer in my code.


If you're talking about getting the position of the get or put pointer within a file, these are returned with the tellp() and tellg() methods associated with file streams. tellp() returns the position of the put pointer for output file streams, and tellg() returns the position of the get pointer for input file streams. Given the file stream named file_op that you have, the syntax for these would be as follows:
int get_pointer_position=op_file.tellg();
int put_pointer_position=op_file.tellp();


Hope that helps.

-jjh
Was This Post Helpful? 0
  • +
  • -

#8 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 02 November 2007 - 08:56 PM

View Postjjhaag, on 2 Nov, 2007 - 10:42 AM, said:

View Postprads, on 1 Nov, 2007 - 10:30 PM, said:

Thanks for ur help but unfortunately both the above programs are not working correctly. I just want to know th position of the pointer in my code.


If you're talking about getting the position of the get or put pointer within a file, these are returned with the tellp() and tellg() methods associated with file streams. tellp() returns the position of the put pointer for output file streams, and tellg() returns the position of the get pointer for input file streams. Given the file stream named file_op that you have, the syntax for these would be as follows:
int get_pointer_position=op_file.tellg();
int put_pointer_position=op_file.tellp();


Hope that helps.

-jjh


Thanks that works......but i have to use cout<<"position is"<<position-1<<endl;
i.e (position-1) bcoz it returns 1 extra position. Is this ok.
Also how do i use fseek();. Pls use the same variables and explain.
Thanks a million,
Prads
Was This Post Helpful? 0
  • +
  • -

#9 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 02 November 2007 - 09:05 PM

Thanks that works......but i have to use cout<<"position is"<<position-1<<endl;
i.e (position-1) bcoz it returns 1 extra position. Is this ok.
Also how do i use fseek();. Pls use the same variables and explain.
Thanks a million,
Prads
[/quote]

And also how do I allocate dynamic memory for the file stream.....using malloc or calloc.....pls write the code as im a little poor in doing that....Thanks,
prads
Was This Post Helpful? 0
  • +
  • -

#10 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: ftell() in C++

Posted 02 November 2007 - 09:30 PM

View Postprads, on 2 Nov, 2007 - 10:05 PM, said:

Also how do i use fseek();. Pls use the same variables and explain.

Please see this post, which is a response to the exact same question you asked in another thread.


Quote

And also how do I allocate dynamic memory for the file stream.....using malloc or calloc.....pls write the code as im a little poor in doing that....

file streams require no manual allocation, and malloc is generally considered to be a C-language contruct, not C++. And as for the second part, it is policy on all of the programming forums to not do other people's work for them. We will help if you have tried to do it and are stuck, and if you post your source code, but not otherwise. This rule is in place both to encourage good learning strategies and to prevent cheating.
Was This Post Helpful? 0
  • +
  • -

#11 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 02 November 2007 - 10:15 PM

Quote

And also how do I allocate dynamic memory for the file stream.....using malloc or calloc.....pls write the code as im a little poor in doing that....

file streams require no manual allocation, and malloc is generally considered to be a C-language contruct, not C++. And as for the second part, it is policy on all of the programming forums to not do other people's work for them. We will help if you have tried to do it and are stuck, and if you post your source code, but not otherwise. This rule is in place both to encourage good learning strategies and to prevent cheating.
[/quote]

Oh!........so filestream allocates memory dynamically and use of malloc or calloc is not reqd?
also in my code file_op is my fid right? (which is similar to pFile in File *pFile)?
and thanks i read fseek()........and as per that should i write it as:
fseek(file_op,offset,SEEK_SET); for fseek(fid,offset,bof); ??
Thanks for ur patience.
prads
Was This Post Helpful? 0
  • +
  • -

#12 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: ftell() in C++

Posted 02 November 2007 - 10:39 PM

fstreams don't require manual allocation in the process of creating a stream. However, if you're reading into a raw array, you will either need to allocate sufficient memory statically to hold all the elements that you'll potentially need, or dynamically allocate for a number found at runtime based on the file contents. If you're reading into an STL string, allocation again takes place behind the scenes.

There is a slightly bigger problem here. You've got a bunch of different posts going right now, all related to the same general topic, which is generally a bad idea because it's on the edge of breaking forum rules. It's especially bad if you're using C in some of them and C++ in others, because although much of the C language will work in C++, many features of C++ are not compatible with C. So you should figure out if you're going to be using file streams (only C++) or pointers to files (mostly C) for access. In this case, your first post dealt with C++ code using fstreams. However, the fseek and ftell functions are for use with file pointers, and are generally limited in use to C. They won't work with C++ file streams. For file streams, you need to use the seekg/seekp and tellg/tellp methods associated with the file stream object.

-jjh

This post has been edited by jjhaag: 02 November 2007 - 10:40 PM

Was This Post Helpful? 0
  • +
  • -

#13 prads  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 113
  • Joined: 22-October 07

Re: ftell() in C++

Posted 03 November 2007 - 12:53 AM

View Postjjhaag, on 2 Nov, 2007 - 10:39 PM, said:

fstreams don't require manual allocation in the process of creating a stream. However, if you're reading into a raw array, you will either need to allocate sufficient memory statically to hold all the elements that you'll potentially need, or dynamically allocate for a number found at runtime based on the file contents. If you're reading into an STL string, allocation again takes place behind the scenes.

There is a slightly bigger problem here. You've got a bunch of different posts going right now, all related to the same general topic, which is generally a bad idea because it's on the edge of breaking forum rules. It's especially bad if you're using C in some of them and C++ in others, because although much of the C language will work in C++, many features of C++ are not compatible with C. So you should figure out if you're going to be using file streams (only C++) or pointers to files (mostly C) for access. In this case, your first post dealt with C++ code using fstreams. However, the fseek and ftell functions are for use with file pointers, and are generally limited in use to C. They won't work with C++ file streams. For file streams, you need to use the seekg/seekp and tellg/tellp methods associated with the file stream object.

-jjh


I am sorry but breaking the forum's rules was not my intent. I need the C++ code and not C code. I checked it on google and whenever i type the function and ask for C++, unfortunately it gives me in C nd i m confused with having both th codes and mixing it. I also dont know all the equivalents of C functions in C++ which is causing the problem. Pls see if u can help me. I have posted the C++ code and there are many errors. This is what i want to do : I am writing a code for tracking of GPS signals and it has to read the acquired data. I want to open a file whose content is unknown and hence has to be dynamically allocated. It has to use the C++ equivalents of fseek();, ftell(), and fread();. Pls help me with this and i'll be grateful to you.
Thanks,
prads
Was This Post Helpful? 0
  • +
  • -

#14 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: ftell() in C++

Posted 03 November 2007 - 01:38 AM

The distinction between C and C++ can be a little blurry sometimes. The functions that you've apparently been searching for are part of the C standard library, available through stdlib.h. They also have basically identical functions in C++, through cstdlib. But those functions only apply in situations where you are using the C++ analogs of the C constructs. So it's okay to use file pointers and functions like fseek and ftell in C++, but only when you're dealing with files accessed through file pointers. When you're using file streams (a strictly C++ construct), you have to use the methods associated with those fstream objects to perform input/output operations.

To seek to a particular point in an input file stream, you would use the seekg() method. To get the current location of the "get" pointer within that file, you would use the tellg() method.

For reading from a file, you have a lot of options. In this case, since you appear to want to read in an entire line of data at once, the getline() method is probably best. In this case, I would use the getline method associated with the STL string, and read into a string from the file:
fstream infile("infileName.txt",ios::in);
string str;
getline (infile,str,'\n');


You could also use the form of getline() that calls for a character array argument; however, in that case you would need to allocate sufficient space for that array by either doing it at compile-time by choosing a HUGE number of elements and hoping you don't read in any more data than the array can hold, or by advancing the get pointer to the end of the line, using the tellg() method to tell you how many bytes have been read, and allocating the memory dynamically. However, the STL string in C++ handles all of the memory allocation behind-the-scenes, so you don't really need to worry about it, and I would recommend the string method because of that.

Then you'll actually have to go about interpreting the data that you've read in. However, that's a few steps away for you yet, and you haven't given any indication of what the data is going to consist of or what you're going to be using it for, so I'm not going to make any recommendations at this time.

And as for the rules - it's no problem right now. Just avoid making multiple posts on the same code; reply in your original thread both so that the board doesn't get cluttered, and so both you and other members can keep track of the development of the code. It's tough having to jump back and forth between multiple threads to figure out what's going on, and most people just aren't going to put in the effort.

Of course, if it has to do with a new problem related to new code, of course go ahead and start a new thread (but only after you've done a search of previous posts for a solution and have checked over the snippets and tutorials and such for an answer).

Hope that helps :)

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1