how to increment pointer in a file

P

pooja

Hi
I m pooja.I m working on a prj in C.
I m unable to increment a char pointer in a file, it gives the same
address as that of the first char.Eg if # is on loc 3000 then i,n ,c
,l,u,d,e are on same address.Can any one plz help .
 
W

Wolfgang Kaufmann

* Thus spoke pooja <[email protected]>:

Hallo,
I m unable to increment a char pointer in a file, it gives the same
address as that of the first char.Eg if # is on loc 3000 then i,n ,c
,l,u,d,e are on same address.Can any one plz help .

Show us your code.


Wolfgang.
 
M

Martin Ambuhl

pooja said:
Hi
I m pooja.I m working on a prj in C.
I m unable to increment a char pointer in a file, it gives the same
address as that of the first char.Eg if # is on loc 3000 then i,n ,c
,l,u,d,e are on same address.Can any one plz help .

You sound like a Pascal programmer. Forget anything you learned about
pointers to i/o streams.

With an input file "foo" with one line
#include
run this and see what happens:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* The following function need not do anything useful. fpos_t is not
necessarily an arithmetic type. */
void showfpos(fpos_t * x)
{
if (sizeof(fpos_t) == sizeof(char)) {
unsigned char y;
memmove(&y, x, sizeof(char));
printf("fpos_t %u:", y);
return;
}
if (sizeof(fpos_t) == sizeof(int)) {
unsigned int y;
memmove(&y, x, sizeof(int));
printf("fpos_t %u:", y);
return;
}
if (sizeof(fpos_t) == sizeof(long)) {
unsigned long y;
memmove(&y, x, sizeof(long));
printf("fpos_t %lu:", y);
return;
}
if (sizeof(fpos_t) == sizeof(long long)) {
unsigned long long y;
memmove(&y, x, sizeof(long long));
printf("fpos_t %llu:", y);
return;
}
printf("fpos_t ?:");
}

int main(void)
{
FILE *f;
size_t nread;
char c;
fpos_t loc;
printf("Trying with ftell\n");
if (!(f = fopen("./foo", "rb"))) {
fprintf(stderr, "could not open 'foo' for reading\n");
exit(EXIT_FAILURE);
}
printf("ftell %ld after open\n", ftell(f));
while (fread(&c, 1, 1, f))
printf("ftell %ld after getting %c\n", ftell(f), c);
printf("ftell %ld after fread failed\n", ftell(f));
fclose(f);


printf("Trying with fgetpos\n");
if (!(f = fopen("./foo", "rb"))) {
fprintf(stderr, "could not open 'foo' for reading\n");
exit(EXIT_FAILURE);
}
fgetpos(f, &loc);
showfpos(&loc);
printf("after open\n");
while (fread(&c, 1, 1, f)) {
fgetpos(f, &loc);
showfpos(&loc);
printf(" after getting %c\n", c);
}
fgetpos(f, &loc);
showfpos(&loc);
printf(" after fread failed\n", ftell(f));
fclose(f);
return 0;
}

Trying with ftell
ftell 0 after open
ftell 1 after getting #
ftell 2 after getting i
ftell 3 after getting n
ftell 4 after getting c
ftell 5 after getting l
ftell 6 after getting u
ftell 7 after getting d
ftell 8 after getting e
ftell 9 after getting

ftell 10 after getting

ftell 10 after fread failed
Trying with fgetpos
fpos_t 0:after open
fpos_t 1: after getting #
fpos_t 2: after getting i
fpos_t 3: after getting n
fpos_t 4: after getting c
fpos_t 5: after getting l
fpos_t 6: after getting u
fpos_t 7: after getting d
fpos_t 8: after getting e
fpos_t 9: after getting

fpos_t 10: after getting

fpos_t 10: after fread failed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top