fscanf简单使用
生活随笔
收集整理的這篇文章主要介紹了
fscanf简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡單使用下fscanf
#include <stdio.h> #include <stdlib.h> int main() {int num, number;char name[20];double height;FILE *fp;fp = fopen("test.txt", "r+");//“r+” 以可讀寫方式打開文件,該文件必須存在,d:\\1.txt表示d盤根目錄下的1.txt文件if (fp == NULL){printf("Cannot open the file!\n");exit(0);}printf(" 學號\t姓名\t身高\n");while (!feof(fp)){number = fscanf(fp, "%d %s %lf", &num, name, &height);if(number != 3){continue;}printf("%d\t%s\t%g\n", num, name, height);}printf("\n");fclose(fp);//要記得關閉文件return 0; }下面是
test.txt對應好上面的代碼
18 張三 1.74 19 李二 1.84 20 王八羔子 1.9721 我要睡覺 1.99?
總結
以上是生活随笔為你收集整理的fscanf简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 并发小程序
- 下一篇: u盘打开以后发现文件变乱码的解决方法