C语言中fscanf的函数
㈠ c语言读取文件函数fscanf()问题。
if(fscanf(fp1,"%s : %s equal: %lf",person[index].num,person[index].first, &person[index].equal)==EOF)
{
printf("END FILE.");
exit(0);
}
}
for(i=0;i<1;i++)
{
//person[i].first[0]=toupper(person[i].first[0]);
//person[i].last[0]=toupper(person[i].last[0]);
printf("The num and name:\n%s:%s equal:%lf.\n",person[i].num,person[i].first, person[i].equal);
data.txt里的数据我是这样的。
414314 : FanXiang equal:2.0
问你个问题"data.txt" 这个文件的路径是在哪?
㈡ 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(fp,"%*[^\n]")为什么可以跳过全部字符直到下一个换行符
%*[^ ]这个通配符的意思,就是跳过所有字符,直到换行符为止。
scanf是格式输入函数,功能是在屏幕上输入指定的信息。简单的来说和printf相似却不相同。
scanf调用格式: scanf("<格式化字符串>",<地址表>);
如:
#include <stdio.h>
int main()
{
int a,b,c;
printf(“input three data: ”);//使用 scanf 之前先用 printf 提示输入。
scanf("%d%d%d",&a,&b,&c); // scanf 的“输入参数”中,前面的取地址符&记住
printf("%d,%d,%d/n",a,b,c);
return 0;
}
(3)C语言中fscanf的函数扩展阅读:
C语言通配符:
%a,%A:读入一个浮点值(仅C99有效)
%c:读入一个字符
%d:读入十进制整数
%i:读入十进制,八进制,十六进制整数
%o:读入八进制整数
%x,%X:读入十六进制整数
%s:读入一个字符串,遇空格、制表符或换行符结束。
%f,%F,%e,%E,%g,%G:用来输入实数,可以用小数形式或指数形式输入。
%p:读入一个指针
%u:读入一个无符号十进制整数
%n:至此已读入值的等价字符数
%[]:扫描字符集合
%%:读%符号
㈣ 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函数使用
没打看懂你说的什么意思, 只是感觉你写的有个地方有问题,你在往文件中写的时候fprintf(f1,"%d\n",temp);是每个字后面都一个回车, 而在读的时候又按照fscanf(f1,"%d",&temp); 这种没有回车的形式读的 ,当然会出问题了, 我帮着改了一下, 你看下行不...
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i;
char temp;
FILE *f1; // source file (bmp)
if((f1=fopen("write.hex","w"))==NULL)
{
printf("f1 error\n");
exit(0);
}
for (i=0;i<8;i++)
{
temp = i;
fprintf(f1,"%d",temp);
}
fclose(f1);
if((f1=fopen("write.hex","r"))==NULL)
{
printf("f1 error\n");
exit(0);
}
for (i=0;i<100;i++)
fscanf(f1,"%d",&temp);
fclose(f1);
}
㈥ C语言用fscanf()函数如何读取文件全部内容
void read_txt(const char* Input, const char* Output){
FILE *fin = fopen(Input, "rb");//以二进制读入
FILE *fout = fopen(Output, "w");
unsigned char ch1,ch2;
while(fscanf(fin, "%c%c", &ch1,ch2) != EOF){//直到文件结束
fprintf(fout, "%d%d", ch1,ch2);//以10进制输出
}
}
int main(){
read_txt("D:/IN.txt","D:/OUT.txt");//txt文件目录
return 0;
}
注:判断文件结束处的语句:fscanf(fin, "%c%c", &ch1,ch2)。其中两个%c之间不能加空格,否则读到的二进制文件会不完整,比源文件少好多个字节
㈦ c语言中的fscanf()函数
是以空格分隔的。
fscanf会返回读取成功的数据个数,你可以用
int ans=fscanf(fp,"%d%d",&a,&b);
的方法,看ans是否为2,是则表示输入成功。
文件指针是自动递增的
㈧ 求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);
}
㈨ C语言中fscanf()函数
fscanf(stdin,"%s%s%s",&s1,&s2,&s3);
C语言中输入数据,都是要加取地址符的亲……
㈩ 请问C语言fscanf的用法
int fscanf(
FILE *stream,
const char *format [,
argument ]...
);
Parameters
stream
Pointer to FILE structure.
format
Format-control string.
Return Value
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 is EOF for fscanf.
If execution is allowed to continue, the function returns EOF and set errno to EINVAL
The fscanf function reads data from the current position of stream into the locations given by argument (if any). Each argument must be a pointer to a variable of a type that corresponds to a type specifier in format. format controls the interpretation of the input fields and has the same form and function as the format argument for scanf; see scanf for a description of format.
// compile with: /W3
// This program writes formatted
// data to a file. It then uses fscanf to
// read the various data back from the file.
#include <stdio.h>
FILE *stream;
int main( void )
{
long l;
float fp;
char s[81];
char c;
if( fopen_s( &stream, "fscanf.out", "w+" ) != 0 )
printf( "The file fscanf.out was not opened\n" );
else
{
fprintf( stream, "%s %ld %f%c", "a-string",
65000, 3.14159, 'x' );
// Set pointer to beginning of file:
fseek( stream, 0L, SEEK_SET );
// Read data back from file:
fscanf( stream, "%s", s ); // C4996
fscanf( stream, "%ld", &l ); // C4996
fscanf( stream, "%f", &fp ); // C4996
fscanf( stream, "%c", &c ); // C4996
// Output data read:
printf( "%s\n", s );
printf( "%ld\n", l );
printf( "%f\n", fp );
printf( "%c\n", c );
fclose( stream );
}
}