help with fseek


 
Thread Tools Search this Thread
Top Forums Programming help with fseek
# 1  
Old 08-16-2009
Power help with fseek

Hi,

I working a c project in IBM AIX. I have a requirement like this.

I have some contents in *temp. I am writing the contents of *temp to a file pointer ftemp (*ftemp for an tmp file tmpfile.txt)

Now I want to read the contents of *ftemp in the reverse order an dneed to print it in the screen.

say if *temp="979899", in ftemp also it will be 979899. From ftemp I need to read the file from the reverse direction. For that I used fseek as below

fseek(ftemp,0,SEEK_END) - this will place the pointer at the end. Now how to read it till the beginning, which function to use.. The contents from ftemp should be read in reverse directiona nd need to be printed in stdout.

Please help me, this is a kind of urgent requirement.

Thx in advance.
# 2  
Old 08-16-2009
read the whole file into a buffer, then print each character in the buffer back to front:
Code:
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>

void backward(FILE *in)
{
	size_t len=0;
	char *p=NULL;
	int i=0;
	struct stat st;
	
	fstat(fileno(in), &st);
	len=st.st_size;
	p=calloc(1, len + 1);
	rewind(in);
	fread(p, 1, len, in);
	for(i=len ; i; i--)
		printf("%c\n", p[i]);
	free(p);	
}

there is no error checking in this code...
# 3  
Old 08-16-2009
Thank you Jim
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Programming

help with reading a binary file and fseek

this is my code and no matter what record number the user enters i cant get any of the records fields to read into the structure acct. What am i doing wrong? #include <stdio.h> typedef struct { char name; int number; float balance; } acct_info_t; int main (int... (0 Replies)
Discussion started by: bjhum33
0 Replies

2. Programming

q on fseek() & ftell() + tga files

In determining the true file size of a .tga file is the fseek(fptr,0,SEEK_END); file_size = ftell(fptr); combination reliable ? The reason Im asking is because the value of file_size is in agreement with doing a wc -c mytga.tga however when I get the value of the extension... (2 Replies)
Discussion started by: JamesGoh
2 Replies

3. Programming

error when using fseek() function

The errors EBADF & ESPIPE occur at this fseek call. Does anybody know how to solve this problem? Thanks in advance. toFileStream=fdopen(localFileDes,"ra+"); if(fseek(toFileStream, 0, SEEK_END)!=0){ if(errno==EBADF) printf("errno==EBADF\n"); if(errno==EINVAL)... (3 Replies)
Discussion started by: ivancheung
3 Replies
Login or Register to Ask a Question