什么是标准输入以及它如何与 fscanf 一起使用?
What is stdin and how is it being used with fscanf?
我不明白 stdin 和 fscanf 之间的联系
struct musteri{
int no;
char name[40];
char surname[25];
double arrear;
};
int main() {
struct musteri hesapBilgi={0,"","",0.0};
FILE *ptr;
if((ptr=fopen("eleman.txt","r+"))==NULL){
printf("error");
}
else{
printf("\n enter a no if you want exit enter 0 -->");
scanf("%d",&hesapBilgi.no);
scanf 获取输入并将 no 放入 sturct musteri
while(hesapBilgi.hesapno !=0){
printf("enter a surname name and arrear --->");
fscanf(stdin,"%s%s%lf",hesapBilgi.surname,hesapBilgi.name,&hesapBilgi.arrear);
这里是 fscanf 从文件中读取数据吗?还是其他事情发生了?
fseek(ptr,(hesapBilgi.no-1)*,sizeof(struct musteri),SEEK_SET);
fseek 在做什么?
fwrite(&hesapBilgi,sizeof(struct musteri),1,ptr);
printf("enter a no :");
scanf("%d",&hesapBilgi.no);
}
fclose(ptr);
}
return 0;
}
int fscanf ( FILE * stream, const char * format, ... );
它从流中读取格式化输入。
stdin 是标准输入流
并且 fseek 用于将与流关联的位置指示符设置为新位置。
SEEK_SET 是一个标志,用于从文件开头设置位置
fseek 示例
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "wb" );
fputs ( "Fseek Hello World." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( "no" , pFile );
fclose ( pFile );
return 0;
}
输出:Fseek Helno World
来自文档 (man scanf
):
The scanf()
function reads input from the standard input stream stdin, fscanf([FILE * stream, ...])
reads input from the stream pointer stream [...]
stdin
是一个 FILE*
。它是一个输入流。
来自文档 (man stdin
)
Under normal circumstances every UNIX program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user's terminal [...]
所以
scanf( ...
实际上等同于
fscanf(stdin, ...
我不明白 stdin 和 fscanf 之间的联系
struct musteri{
int no;
char name[40];
char surname[25];
double arrear;
};
int main() {
struct musteri hesapBilgi={0,"","",0.0};
FILE *ptr;
if((ptr=fopen("eleman.txt","r+"))==NULL){
printf("error");
}
else{
printf("\n enter a no if you want exit enter 0 -->");
scanf("%d",&hesapBilgi.no);
scanf 获取输入并将 no 放入 sturct musteri
while(hesapBilgi.hesapno !=0){
printf("enter a surname name and arrear --->");
fscanf(stdin,"%s%s%lf",hesapBilgi.surname,hesapBilgi.name,&hesapBilgi.arrear);
这里是 fscanf 从文件中读取数据吗?还是其他事情发生了?
fseek(ptr,(hesapBilgi.no-1)*,sizeof(struct musteri),SEEK_SET);
fseek 在做什么?
fwrite(&hesapBilgi,sizeof(struct musteri),1,ptr);
printf("enter a no :");
scanf("%d",&hesapBilgi.no);
}
fclose(ptr);
}
return 0;
}
int fscanf ( FILE * stream, const char * format, ... );
它从流中读取格式化输入。 stdin 是标准输入流
并且 fseek 用于将与流关联的位置指示符设置为新位置。
SEEK_SET 是一个标志,用于从文件开头设置位置
fseek 示例
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile = fopen ( "example.txt" , "wb" );
fputs ( "Fseek Hello World." , pFile );
fseek ( pFile , 9 , SEEK_SET );
fputs ( "no" , pFile );
fclose ( pFile );
return 0;
}
输出:Fseek Helno World
来自文档 (man scanf
):
The
scanf()
function reads input from the standard input stream stdin,fscanf([FILE * stream, ...])
reads input from the stream pointer stream [...]
stdin
是一个 FILE*
。它是一个输入流。
来自文档 (man stdin
)
Under normal circumstances every UNIX program has three streams opened for it when it starts up, one for input, one for output, and one for printing diagnostic or error messages. These are typically attached to the user's terminal [...]
所以
scanf( ...
实际上等同于
fscanf(stdin, ...