c++使用*lec=fopen();修复语法调试;

c++ fix syntax debug with *lec=fopen();

本文关键字:语法 调试 使用 lec fopen c++      更新时间:2023-10-16

我正在和一个比我更了解的朋友一起编写一些C++代码,并且代码有一个错误,我想帮助解决这个问题,因为我不知道如何。。。

编辑编译器在第59行停止,其中写入:FILE *ecr("result.txt","wt");

还有很多其他事情需要修复,我修复到49(现在59;))我又被屏蔽了。。。

谢谢!

编辑(再次抱歉)带有消息:

C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp||In function 'int main()':|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|59|error: expression list treated as compound expression in initializer [-fpermissive]|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|59|error: cannot convert 'const char*' to 'FILE* {aka _iobuf*}' in initialization|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|82|error: expected initializer before '<' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|82|error: expected ';' before '<' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|82|error: expected primary-expression before '<' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|84|error: expected ')' before ']' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|84|error: expected ')' before ']' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|84|error: expected primary-expression before ']' token|
C:UsersadDesktopPYTHONjoshjoshcolor3dsolution_to_array.cpp|84|error: expected ';' before ']' token|
||=== Build finished: 9 errors, 0 warnings (0 minutes, 0 seconds) ===|

如果有人能想办法解决这个问题,那就太好了!

代码:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {
int COL = 0;
int LIN = 0;
cout << endl << "columns?" << endl;
cin  >> COL;
cout << "lines?" << endl;
cin  >> LIN;
int CELL = LIN*COL;
cout << CELL << " cells" << endl;
//1- reading the file priority.csv
char Coul[COL][LIN];
for (int i=0; i<COL; i++) Coul[i][0]='';
FILE *lec=fopen("priority.csv","rt");
if (lec == NULL) {
        printf("Error");
        return(0);
}
char ligne[120];
int N;
for (int i=0; i<COL; i++)
{
  char nom[8];
  fgets(ligne,15,lec);
  sscanf(ligne,"%d,%s",&N,nom);
  if (N == i) strcpy(Coul[i], nom);
  else {printf("Error");
  break;
  }
}
fclose(lec);
//2- reading the file solution.txt and writing the result.
lec=fopen("solution.txt","rt"); // FIXED HERE
if (lec == NULL) {printf("Error");
    return(0);
    }
FILE *ecr=fopen("result.txt","wt"); //STOPS HERE
for (int i=1; i<CELL; i++)
{
  char c[4][8];  // 4 color names
  float v[4]; // 4 values
  fgets(ligne,119,lec);
  sscanf(ligne,"%d,%s,%f,%s,%f,%s,%f,%s,%f",
              &N, c[0], &v[0], c[1], &v[1], c[2], &v[2], c[3], &v[3]);
  if (N == i)
  {
    if (strlen(c[0]) == 0)  // la ligne est vide
    {
      fprintf(ecr,"%d ",i);
      for (int i=0; i<COL; i++) fprintf(ecr,"0 ");
      fprintf(ecr,"n");
    }
    else
    {
      fprintf(ecr,"%d ",i);
      int r[4];
// we search the rang of c[ordre]
      for (int ordre=0; ordre<4; ordre++)
      {
        for (int ir=0, ir<COL; ir++)
        {
          if (strcmp(c(ordre], Coul[ir]) == 0) r[ordre]=ir;
        }
      }
      for (int ir=0; ir<r[0]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[0]);
      for (int ir=r[0]+1; ir<r[1]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[1]);
      for (int ir=r[1]+1; ir<r[2]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[2]);
      for (int ir=r[2]+1; ir<r[3]; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"%d ",v[3]);
      for (int ir=r[3]+1; ir<57; ir++) fprintf(ecr,"0 ");
      fprintf(ecr,"n");
    }
  }
  else {printf("Error"); break;}
}
fclose (ecr);
fclose(lec);
}

如果您遇到几个错误,有时也值得查看附近的错误,看看它们是否有帮助。

..._to_array.cpp|53|error: redeclaration of 'FILE* lec'|
..._to_array.cpp|28|error: 'FILE* lec' previously declared here|

所以,它认为你重新申报了什么。没错。

FILE *lec=fopen("priority.csv","rt");
//...
FILE *lec=fopen("solution.txt","rt");

您可以将其更改为

FILE *lec=fopen("priority.csv","rt");
//...
lec=fopen("solution.txt","rt");

您可能需要考虑将这个长函数拆分为一些较小的函数,这样您就更有可能发现错误


编辑

 FILE *ecr("result.txt","wt"); 

可能意味着打开一个文件,就像你前面的行所做的那样:

 FILE *ecr=fopen("result.txt","wt"); 

编辑所以,现在你修复了

for (int ir=0, ir<COL; ir++)

=>

for (int ir=0; ir<COL; ir++) //semicolon not comma
//           ^^

下一个

if (strcmp(c(ordre], Coul[ir]) == 0) r[ordre]=ir;

=>

if (strcmp(c[ordre], Coul[ir]) == 0) r[ordre]=ir; // [ not (
//          ^^

应该这样。不过我不敢运行它——它也不例外安全。当你遇到很多错误时不要惊慌-仔细阅读

lec似乎声明了两次,所以第二次调用应该是

lec=fopen("solution.txt","rt");

因为你已经有了高于的FILE* lec

你确定这就是你正在编译的代码吗?第50行是fclose(lec),它没有提到fopen

剩下的是对lec[53]的重新声明、对fopen[59]的丢失调用、应为";"(分号)[82]的","(逗号)和应为"["[84]的"("。