#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stdafx.h"
void getpara(char filename[40]) {
FILE *fp;
char buf[1024];
fp = fopen(filename, "r");
if (fp == NULL) {
printf("open error!\n");
}
else {
while (fgets(buf, sizeof(buf), fp) != NULL) {
printf(buf);
}
}
}
void main() {
char file[40] = "F:\\C程序\\getfile\\in.txt";
getpara(file);
#include<stdlib.h>
#include<string.h>
#include"stdafx.h"
void getpara(char filename[40]) {
FILE *fp;
char buf[1024];
fp = fopen(filename, "r");
if (fp == NULL) {
printf("open error!\n");
}
else {
while (fgets(buf, sizeof(buf), fp) != NULL) {
printf(buf);
}
}
}
void main() {
char file[40] = "F:\\C程序\\getfile\\in.txt";
getpara(file);
}
Tip:1.在使用fopen()函数时,可能会出现this function may be unsafe 的错误,需在该项目的属性中的预处理器定义中计 入_CRT_SECURE_NO_WARNINGS;
2.在使用绝对路径时记住要使用转义字符:\\