Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c语言:以数字形式读取文件内容并将其相加_C_Parsing_File - Fatal编程技术网

c语言:以数字形式读取文件内容并将其相加

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 )

我在一个名为: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 ) { 
  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[()/代码>和 SCANSF()> <代码> >

< P> > C++中的抱歉:(我不知道c

这是一个非常简单的简单思维逻辑代码:我也是一个初学者,我还没有测试过这个程序,所以很抱歉,如果出了什么问题,但准确地说 基于同样的原理,我的解析器工作正常。所以这是一个真正的方法。不是很有效,但。。。 不要直接使用这个程序,理解它的逻辑,这将对你有很大帮助。复制它不会给你任何东西 …语法分析器导师太少了

int x=0; char ch='r';//我使用这个等式来避免在ch的第一次检查时出错。 当程序启动时,它必须由某些东西填充。 char-bigch[10]; int checknumber=0

float firstnumber=0; 浮点数=0; 浮动结果=0

void clear(char frombigar[10],int xar)//此函数获取bigch作为引用,这意味着 此处所做的更改将直接影响bigch本身。 函数获取数组的实际长度并放置空格 在比奇的每一个元素中,我们要把数字归零,我们需要清除 任何以前的数字的比奇。下面你会看到为什么我需要这个。 “xar”是主函数中的x。它在这里告诉我们的清洁器 填充bigar元素的真实长度。 { 对于(int i=0;i }

}

int main() {
谢谢你的回答…我知道如何在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.
            }