本文目录一览

1,c语言fprintf的用法

第一个参数表示输出位置。stdout是标准输出,一般就是控制台。改为文件指针的话,就会输出到文件。后2个参数与printf的参数是一样的。

c语言fprintf的用法

2,fprintf的用法

fprintf(fp1,"%d",iter);int i;for(i=0; i<N; i++) fprintf(fp1, " %lf", p[i]);fprintf(fp1,"\n");

fprintf的用法

3,fprintf和fscanf的用法

这个问题不好办,因为第一个程序里写入文件的时候字符串和数字之间以逗号分隔,那么第二个程序fscanf输入%s的时候是不会识别逗号的,它只会把hello,100作为一个整体字符串输入到str2中。因此str2就是hello,100。然后再输入%d的时候已经没有东西了,因此b仍然为0。所以最后输出hello,1000就是hello,100和最后那个0组成的。要解决的话只能是把第一个程序里的 fprintf(fp, "%s,%d", str,a);改成 fprintf(fp, "%s %d", str,a);也就是文件中以空格分隔字符串和数字。 刚才发现还有一种解决方法。。。那就是第一个程序不改,而把第二个程序的fscanf(fp,"%s%d",str2,&amp;b);改成如下三行:fscanf(fp,"%[^,]",str2);fgetc(fp);fscanf(fp,"%d",&amp;b);第一行的%[^,]是fscanf的格式控制,意为输入字符串并且以逗号为分隔符,就是说遇到逗号就结束且不读入这个逗号。因此读到的str2就是hello第二行读入一个字符,就是那个逗号第三行再读入一个数,就读入了100这样就实现了以逗号分隔字符串和数字时,依然能够正确的读文件!

fprintf和fscanf的用法

4,fprintf函数是什么

fprintf是向文件输出,将输出的内容输出到硬盘上的文件、相当于文件的设备上
fprintf是c/c++中的一个格式化写 库函数;其作用是格式化输出到一个流/文件中;函数原型:int fprintf (file* stream, const char*format, [argument])参数:file*stream为文件指针,const char* format以什么样的格式输出,[argument]为输入列表返回值:printf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.实例:#include int main(void) { file *in,*out; in = fopen("\\autoexec.bat", "rt"); if(in == null) { fprintf(stderr, "can not open inputfile.\n"); return 1; } out = fopen("\\autoexec.bat", "wt"); if(out == null) { fprintf(stderr, "can not open outputfile.\n"); return 1; } while(!feof(in)) fputc(fgetc(in), out); fclose(in); fclose(out); return 0; }

5,fprintf函数是什么

类似printf,区别是写入文件而不是屏幕(标准输出)
其实 printf("hello world"); 就是fprintf(stdout, "hello world");变体。其比printf多一个参数指定输出为stdout也就standard output data stream(标准数据输出流).如果你想对错误做输出 可以fprintf(stderr, "你这个错误是:%s", "xxoo");.printf只是fprintf 输出流为stdout的一个具体的列子罢了。 fprintf可以指定很多输出设备,不光是标准输出,还可以说文件哦。 比如fprintf(filepointer, "这个可要输出一个文件中哦");
fprintf是C/C++中的一个格式化写 库函数;其作用是格式化输出到一个流/文件中;函数原型:int fprintf (FILE* stream, const char*format, [argument])参数:FILE*stream为文件指针,const char* format以什么样的格式输出,[argument]为输入列表返回值:printf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.实例:#include <stdio.h>int main(void) FILE *in,*out; in = fopen("\\AUTOEXEC.BAT", "rt"); if(in == NULL) fprintf(stderr, "Can not open inputfile.\n"); return 1; } out = fopen("\\AUTOEXEC.BAT", "wt"); if(out == NULL) fprintf(stderr, "Can not open outputfile.\n"); return 1; } while(!feof(in)) fputc(fgetc(in), out); fclose(in); fclose(out); return 0;}

文章TAG:fprintf  c语言fprintf的用法  
下一篇