#include<stdio.h>
#include<string.h>
int main()
{
FILE * handle;
char con[100];
//第一種使用fopen的方式,windows系統是直接輸入文件的絕對路徑需要這種方式"e:\\aaa.txt",注意是兩個反斜杠,一個反斜杠的話就錯了
//handle=fopen("e:\aaa.txt","r");
//第二種使用fopen的方式,利用一個字符串來保存文件路徑和名字,運行程序后,在dos下提示你輸入路徑名字,可以輸入"e:\\aaa.txt",
//或者"e:\aaa.txt",也就是說dos下一個反斜杠或者兩個反斜杠都是正確的
//char buf[80];
//printf("please input the filename you want to open:");
//gets(buf);
//handle=fopen(buf,"r");
//利用一個初始化好的字符串也可以正確的使用fopen,以下這幾種初始化方式都是正確的都是兩個反斜杠
//char buf[]="e:\\aaa.txt";const char buf[100]="e:\\aaa.txt";
char *buf="e:\\aaa.txt";
handle=fopen(buf,"r");
if(!handle)
perror("can not open this file\n");
else
printf("you have opened this file successfully!\n");
fgets(con,100,handle);
printf("the content of file is:%s\n",con);
fclose(handle);
return 0;
}
主要介紹了fopen函數的第一個參數的使用,代碼里面說的已經很清楚了,其余類似fopen的函數的使用也一樣