c语言:以数字形式读取文件内容并将其相加
我在一个名为:values.txt的文本文件中有以下内容c语言:以数字形式读取文件内容并将其相加,c,parsing,file,C,Parsing,File,我在一个名为:values.txt的文本文件中有以下内容 1 4 2.5 3.76 122 10 277.543 165.4432 我正在尝试读取此文本文件的内容,并将每两对内容相加,然后输出结果。。。 输出如下所示: 1 Pair:(1, 4) = 5 2 Pair:(2.5, 3.76)= 6.26 等等 我正在像这样打开文件 int c; FILE myfile; myfile= fopen("values.txt", "r"); if ( myfile == NULL )
1 4
2.5 3.76
122 10
277.543
165.4432
我正在尝试读取此文本文件的内容,并将每两对内容相加,然后输出结果。。。
输出如下所示:
1 Pair:(1, 4) = 5
2 Pair:(2.5, 3.76)= 6.26
等等
我正在像这样打开文件
int c;
FILE myfile;
myfile= fopen("values.txt", "r");
if ( myfile == NULL ) {
printf("Cannot open TEXT file\n");
return 1;
}
double aa,bb;
while ( (c = getc(myfile) ) != EOF ) {
// HERE SHOULD I DO THE OUTPUT BUT HOW?
}
非常感谢你的帮助
Language=C对于这个简单的任务,使用双a,b;
double a, b;
if (fscanf(myfile, "%lf %lf", &a, &b) == 2)
printf("%f + %f = %f\n", a, b, a+b);
如果(fscanf(myfile,“%lf%lf”、&a和&b)==2)
printf(“%f+%f=%f\n”,a,b,a+b) 看起来像是家庭作业问题,但
fscanf
可以将字符串读入如下变量:
int n;
fscanf (myfile,"%d",&n);
下面的代码符合您的期望。myfile应声明为FILE*。fopen返回指向文件结构的指针。如果文件非常大,我建议读取较大的缓冲区(例如:65535等),逐字符解析并将其转换为浮点值。它减少了系统调用开销,这比将文本处理为浮点值花费更多的时间
#include <stdio.h>
#include <string.h>
main(int argc, char *argv[])
{
FILE* myfile;
myfile = fopen("values.txt", "r");
if ( myfile == NULL ) {
printf("Cannot open TEXT file\n");
return 1;
}
double aa,bb;
while (2 == fscanf(myfile, "%lf %lf", &aa, &bb)) {
printf("%lf\n", aa+bb);
}
return 0;
}
#包括
#包括
main(int argc,char*argv[])
{
文件*myfile;
myfile=fopen(“values.txt”、“r”);
如果(myfile==NULL){
printf(“无法打开文本文件\n”);
返回1;
}
双aa,bb;
而(2==fscanf(myfile、%lf%lf、&aa和&bb)){
printf(“%lf\n”,aa+bb);
}
返回0;
}
您还没有显示单值行的输出所需的内容,但这看起来像是fgets()
和sscanf()
的情况,除非您确实希望将具有单值的两行作为一个单元进行处理
char buffer[256];
int rownum = 0;
while (fgets(buffer, sizeof(buffer), myfile) != 0)
{
double aa, bb;
int n = sscanf(buffer, "%lf %lf", &aa, &bb);
if (n == 2)
printf("%d Pair:(%g, %g) = %g\n", ++rownum, aa, bb, aa+bb);
else if (n == 1)
printf("%d Solo:(%g) = %g\n", ++rownum, aa, aa);
else
{
printf("Failed to find any numbers in <<%s>>\n", buffer);
}
}
char缓冲区[256];
int rownum=0;
while(fgets(buffer,sizeof(buffer),myfile)!=0)
{
双aa,bb;
int n=sscanf(缓冲区、%lf%lf、&aa和&bb);
如果(n==2)
printf(“%d对:(%g,%g)=%g\n“,++rownum,aa,bb,aa+bb);
else如果(n==1)
printf(“%d Solo:(%g)=%g\n”++rownum,aa,aa);
其他的
{
printf(“在缓冲区\n中找不到任何数字”);
}
}
如果您使用了fscanf(myfile,“%g%g”、&aa和&bb)
,那么它将通过换行(它们计为空白)来查找数字,因此它将从一行读取一个数字,从另一行读取第二个数字。这通常不是人们想要的(但当它是你需要的时候,它是非常有用的)。使用<>代码> fSCANFER()/代码>的错误恢复会比<>代码> fgs[()/代码>和谢谢你的回答…我知道如何在C++中用正确的移位来做>但是我不知道如何在C中做这件事。再次感谢,这不是一个家庭作业……我正在尝试计算从电影中提取的帧之间的时差……因为一些数字看起来是分数的,<代码> %D < /COD>格式和<代码> int <代码>类型。可能不是最佳匹配。为什么使用
return 1;
而不是return 0;
?
if (ch!= " ") //i used space as an indicator where one number ends
//so while space havent been reahced, read letters.
{ bigch[x] = ch; //get read letter into bigch array.
x++; //icrement bigch array step
}
else
if(ch == " ") //if space is reached that means one number has ended and
{ im trying to set a flag at that moment. it will be used further.
checknumber++; the flag is simple number. first space will set checknumber to 1
second space will set it to 2. thats all.
}
if (checknumber == 1) //if our checknumber is 1, wich means that reading
of first number is done, lets make one whole float
from that bigch array.
clearar(bigch,x); //here we send bigch and its element step into function where
bigch gets cleaned because we dont want some ghost numbers in it.
abviously clearar function cleans bigch int main function aswell,
not only in it's teritory. its a global cleaning :)
}
else if (checknumber ==2) //here we do the same but if flag is 2 this means that two spaces
had been passed and its time to convert bigch into secondnumber.
{ secondnumber = atof(bigch); //same method of converting array into float (it hates other
not number letters, i mean if its a number its fine. if in your text
was 'a' or 's' in that case atof will panic hehe.. )
clearar(bigch,x); //same thing, we send bigch to cleaner function to kill any numbers
it, we get one space letter ( " " ) into each element of bigch.
}