当前位置: 代码迷 >> 综合 >> fopen fscanf fprintf函数示例
  详细解决方案

fopen fscanf fprintf函数示例

热度:40   发布时间:2023-09-22 09:33:59.0

fopen fscanf fprintf函数示例

示例如下:

#include <stdio.h>int main ()
{char name [20];int age, length;FILE *fp;fp = fopen ("test.txt","w+");fprintf (fp, "%s %d","Fresh2refresh",5000);length = (int)ftell(fp); // Cursor position is now at the end of file/* You can use fseek(fp, 0, SEEK_END); also to move thecursor to the end of the file */rewind (fp); // It will move cursor position to the beginning of the filefscanf (fp, "%s", name);//这两个fscanf是有顺序的,先age再name就会读不到age,因为指针在文件开始处fscanf (fp, "%d", &age);fclose (fp);printf ("Name: %s \n Age: %d \n",name,age);printf ("Total number of characters in file is %d\n", length);return 0;
}

#fread函数fopen fscanf fprintf函数示例