当前位置:首页 » 编程语言 » c语言fscanf的返回值
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言fscanf的返回值

发布时间: 2022-01-20 05:35:19

c语言fscanf用法

#include<stdio.h>
#include<stdlib.h>
intmain()
{
FILE*fp;
intid;
charstu;
if((fp=fopen("c:/stu.txt","w+"))==NULL)
{
printf("打开文件,出现错误");
exit(1);
}
scanf("%d%s",&id,&stu);
fprintf(fp,"%d %s",id,&stu);
id=0;
stu=0;
rewind(fp);//写完之后不能马上读,需要重置fp到文件头
fscanf(fp,"%d%s",&id,&stu);//少了&
printf("%d %s",id,stu);
return0;
}

Ⅱ c语言中的fscanf()函数

fprintf(fp,"%s,%c,%d,%f",str,a, c, b); 这个输出格式表明 你的文件1.txt 里的数据 是用 逗号 分 隔。
if((fp = fopen("1.txt","r"))==NULL) 你要打开 读 这个 用 逗号为 分隔符 的文件。

fscanf(fp,"%s,%c,%d,%f", str, &a, &c, &b);
漏写 str, 给你补上,但 这仍不能解决 %s, 的逗号分隔问题。

必须 用下面格式读取逗号分隔的数据:
fscanf(fp,"%[^,],%c,%d,%f", str, &a, &c, &b);
======================================

假如文件里的数据 用 空白 分隔,不用 逗号,日子就好过得多:
fprintf(fp,"%s %c %d %f",str,a, c, b);
fscanf(fp,"%s %c %d %f", str, &a, &c, &b);

Ⅲ c中fscanf返回值说明下

:int fscanf( FILE *stream, const char *format, ... );
函数fscanf()以scanf()的执行方式从给出的文件流中读取数据. fscanf()的返回值是事实上已赋值的变量的数,如果未进行任何分配时返回EOF.

Ⅳ C语言中fscanf()函数

fscanf(stdin,"%s%s%s",&s1,&s2,&s3);
C语言中输入数据,都是要加取地址符的亲……

Ⅳ (c语言)文件相关函数的返回值

fgetc():成功时返回读入的字节数。错误或文件尾时返回EOF;
fputc():成功时返回写入的字节数。错误时返回EOF;
fgets():成功时返回字符串地址,错误或0读入时返回NULL;
fputs():成功时返回一个非负整数,错误时返回EOF
fprintf():成功时返回写入的字节数,错误时返回负数(negative value)
fscanf():返回正确读入项目(items)的个数,错误时返回EOF
fwrite():返回正确写入项目的个数,错误时返回0
fread():返回正确读入项目的个数,错误时返回0
fseek():成功返回0,错误返回-1。

Ⅵ fscanf返回什么值

1.返回实际读取的数据个数,出错或者到结尾返回EOF。
函数原型:
DWORD GetMoleFileName(
HMODULE hMole,
LPTSTR lpFilename,
DWORD nSize
);
2.函数参数说明:
hMole HMODULE 装载一个程序实例的句柄。参数为NULL,函数返回当前应用程序全路径。lpFileName LPTSTR 是存放返回内存块的指针,是输出参数DWORD ,装载到缓冲区lpFileName的最大值,strcat是将szBuf与后边的值连接起来。

Ⅶ 求c语言大神!!!关于fscanf读取字符串不解!!!为什么fscanf返回值都是1

逗号不能作为字符串的分隔符,如果可能的话,将文件中的逗号替换为空格,这样就可以达到你的目的了。

Ⅷ C语言fscanf的用法是什么

用 法: int fscanf(FILE *stream, char *format,[argument...])。

1、fscanf()的format参数允许的格式为:“%[*][width][modifiers]type”。[]中的内容根据需要供选,可缺省,%type必须要有,不可省。

2、“format”是C字符串,由“空格”、“非空格”及“转换符”组成。具体格式为%[*][width][modifiers]type,与“format”中“转换符”对应变量地址的列表,两地址间用逗号隔开。

fscanf()对空格的处理示例代码如下:

re=fscanf(pF,"%d%d%d",&oLine.p,&oLine.l,&oLine.x);
if(3==re){
//Printonstdout
printf("%d %d %d ",oLine.p,oLine.l,oLine.x);
}

Ⅸ c语言中,成功调用fprintf fscanf函数后有什么返回值吗

当然有。楼主可看MSDN有详细描述,

fscanf, fwscanf
Read formatted data from a stream.
int fscanf( FILE *stream, const char *format [, argument ]... );
int fwscanf( FILE *stream, const wchar_t *format [, argument ]... );

Return Value // 也就是正常会返回从文件成功写入的变量个数,否则返回EOF
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value isEOFfor fscanf orWEOFforfwscanf.

fprintf, fwprintf
Print formatted data to a stream.
intfprintf(FILE*stream,constchar*format[,argument]...);
intfwprintf(FILE*stream,constwchar_t*format[,argument]...);

Return Value // 也就是正常会返回写入文件的字节数,否则返回负数
fprintfreturns the number of bytes written.fwprintfreturns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs.

Ⅹ 求C语言 fscanf的用法,

你的理解错了,这是将文件中的数据输入到程序中的变量,这个函数是一个输入函数,参考sscanf用法。
sscanf示例如下,得到n=1,sz="asdf"
{
char* str = "1 asdf";
int n;
char sz[10];
sscanf(str, "%d%s", &n, &sz);
printf("%d %s", n, sz);
}
fscanf示例如下,加入pf是指向文件内容为1 asdf的文件指针,得到n=1,sz="asdf"
{
int n;
char sz[10];
sscanf(pf, "%d%s", &n, &sz);
printf("%d %s", n, sz);
}