0

There's some way of get an address of a character of a file? Example:

FILE *fr;
char ch;

fr = fopen("text.txt","r");

while (!feof(fr)) {
  ch = fgetc(fr);
  printf("%c", ch);
}

I want the address of not the variable ch, but of the char stored in ch. Anyone who can help, please can tell me how?

Pablo
  • 13,271
  • 4
  • 39
  • 59
  • The address of the char stored in ch *is* the address of ch… do you mean you want the offset of the character within the file? – nemequ Mar 29 '18 at 23:55
  • 1
    Please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) And please read the man page for `fgetc` which returns an `int` value not `char`. – Weather Vane Mar 30 '18 at 00:00
  • 1
    A character does not have an address, it is typically an `int` value. The variable does have an address. Did you mean to ask about the position in the file a particular character is read from? – Weather Vane Mar 30 '18 at 00:01
  • 1
    You can call `ftell` before reading each character from the file, or count them. – Weather Vane Mar 30 '18 at 00:06

2 Answers2

0

Exactly i can't understand about what you want.

It is just a data which was wrote in ch's area. It is not a literal string. So it is incorrect question that you want address of character data which in ch. I guess Maybe you want each character's offset. If is true, then try use ftell(). ftell() will give you an offset of file where you read. (Offset means the index of where you read from file's first) If you want some another answers then i suggest that you tell your purpose what you want to do ultimately.

summation
  • 142
  • 1
  • 8
0

You can try keeping the counter for the location of the character. See the following:

FILE *fr;
char ch;
int count =0;
fr = fopen("text.txt","r");

while (!feof(fr)) {
  ch = fgetc(fr);
  i++;
  printf("%c", ch);
  printf("Location: %d for the Character: %c",i,ch);
}

I hope this will help. If you want to know which row and column it belongs then my advise for you is to have two integer variable that will act for row and column respectively.