简体   繁体   English

我不明白这个简单的循环如何改变文件指针位置

[英]I don't understand how this simple loop changes filepointer position

This simple program opens file with letters A to Z that i already created, changes one letter to a star *, than prints list of letters again.这个简单的程序打开我已经创建的包含字母 A 到 Z 的文件,将一个字母更改为星号 *,然后再次打印字母列表。 Everything works but i dont understand how this loop works:一切正常,但我不明白这个循环是如何工作的:

for (i = 0; i < 26; i++)
    {
        letter = fgetc(fptr);
        printf("The next letter is %c.\n", letter);
    }

Particularly where does it say that fptr - filepointer is connected or equals to i??特别是它在哪里说 fptr - filepointer 已连接或等于 i? I understand that loop will increase i from 0 to 26 but i have no idea why fptr should increase too??我知道循环会将 i 从 0 增加到 26,但我不知道为什么 fptr 也应该增加?? The whole program is below:整个程序如下:

FILE * fptr;

main()
{
    char letter;
    int i;

    fptr = fopen("//Users//nik//Desktop//letters.txt", "r+");

    if (fptr == 0)
    {
        printf("There was an error while opening the file.\n");
        exit(1);
    }

    printf("Which # letter would you like to change (1-26)? ");
    scanf(" %d", &i);

    // Seeks that position from the beginning of the file

    fseek(fptr, (i-1), SEEK_SET); // Subtract 1 from the position
    // because array starts at 0

    // Write an * over the letter in that position
    fputc('*', fptr);

    // Now jump back to the beginning of the array and print it out

    fseek(fptr, 0, SEEK_SET);

    printf("Here is the file now:\n");

    for (i = 0; i < 26; i++)
    {
        letter = fgetc(fptr);
        printf("The next letter is %c.\n", letter);
    }

    fclose(fptr); 

    return(0);
}

Based on the next source... http://www.cplusplus.com/reference/cstdio/fgetc/基于下一个源... http://www.cplusplus.com/reference/cstdio/fgetc/

This function will return the current character which is pointed by the position indicator and then it will move to the next char.此函数将返回位置指示器指向的当前字符,然后它将移动到下一个字符。 So, it is rational that if you loop 26 times you will call the function 26 times and the function will return you the corresponding letters.因此,如果您循环 26 次,您将调用该函数 26 次并且该函数将返回相应的字母是合理的。

FILE is struct ure with the following field: FILEstruct茜与以下领域:

unsigned char *_p;  /* current position in (some) buffer */

When you open a file, this variable points to a buffer which holds the contents of the file.当你打开一个文件时,这个变量指向一个保存文件内容的缓冲区。 Consider the following program ( main.c ):考虑以下程序( main.c ):

#include <stdio.h>

int main(void)
{
    int ch;
    FILE *pf = fopen("main.c", "r");

    rewind(pf);
    printf("%p\n", pf->_p);

    ch = fgetc(pf);
    printf("%p: %c\n", pf->_p, ch);

    ch = fgetc(pf);
    printf("%p: %c\n", pf->_p, ch);

    fclose(pf);

    return 0;

} }

Executing the program produces the following output (on my machine):执行程序会产生以下输出(在我的机器上):

0x7f7fe1803200
0x7f7fe1803201: #
0x7f7fe1803202: i

As you can see, the address that pf->_p points to is incremented by each call to fgetc .如您所见,每次调用fgetc都会增加pf->_p指向的地址。 How fgetc is called (for example, in a for loop) is irrelevant. fgetc的调用方式(例如,在for循环中)无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM