从.txt C了解 [英] read from .txt C
本文介绍了从.txt C了解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些麻烦用C语言..
I'm having some trouble with C language..
我有形式不同的线条一个文件TXT:
I have one file txt with various lines in the form:
F 65 S 4 1 139.56 3704.26
和我的程序:
p = fopen("dados.txt", "r");
if ( p == NULL) {
printf("\n\nNao foi possivel abrir o arquivo.\n");
exit(1);
}else{
while ( !feof(p) ){
fscanf(p,"%c %d %c %d %d %f %f",
&sexo,&idade,&estadoCivil,&numFilhos,&freq,&mediaGasto,&mediaSalarial);
printf("%c %d %c %d %d %f %f\n",
sexo,idade,estadoCivil,numFilhos,freq,mediaGasto,mediaSalarial);
}
返回的是:
很糟糕...
如果我的fscanf改变:%C到%F
if i change in fscanf: %c to %f
返回的是:
看起来不错,但变量idade始终为0 ...:•
looks great, but the variable idade is always 0... :S
跆拳道我有什么关系?
推荐答案
您必须将新行添加到您的 scanf函数
电话:
You have to add the newline to your scanf
call:
fscanf(p,"%c %d %c %d %d %f %f\n",
&sexo,&idade,&estadoCivil,&numFilhos,&freq,&mediaGasto,&mediaSalarial);
如果没有 scanf函数
换行,第一行是正确的,但下面的行从输入的新行分配给 sexo
。
Without the newline in scanf
, the first line will be correct, but the following line assigns the newline from the input to sexo
.
这篇关于从.txt C了解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文