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 );
}
}