2013-08-07 28 views
2

我正在與一位朋友比較瞭解一些C++代碼,該代碼有一個bug,我想幫助解決這個問題,因爲我無法弄清楚如何......(已解決)C++使用* lec = fopen()修復語法調試;

編輯 編譯器在第59行停止,其中:編寫了FILE *ecr("result.txt","wt");

還有很多其他的事情來解決,我定到49(59現在;))我再次受阻...

謝謝!

編輯(再次,抱歉) 隨着消息:

C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp||In function 'int main()':| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|59|error: expression list treated as compound expression in initializer [-fpermissive]| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|59|error: cannot convert 'const char*' to 'FILE* {aka _iobuf*}' in initialization| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected initializer before '<' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected ';' before '<' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|82|error: expected primary-expression before '<' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected ')' before ']' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected ')' before ']' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_to_array.cpp|84|error: expected primary-expression before ']' token| 
C:\Users\ad\Desktop\PYTHON\josh\josh\color\3d\solution_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]='\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); 
} 
+1

@ H2CO3哦,來吧,停止它......誰在乎變量是否有法國名稱?除此之外你可以閱讀它們,所以什麼??? –

+2

@ bash.d ***即使我說法語,但是非常非常難讀,***(和其他人)都習慣於英語(如果一個人不知道變量名是什麼意思,那麼很難弄清楚代碼的含義......) – 2013-08-07 14:41:54

+0

因此,如果你是一個編碼員/程序員/無論你應該能夠應付那個......你是一個聰明的人,是不是? –

回答

2

lec似乎宣告了兩次,所以第二個電話應該是

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

既然你已經有了FILE* lec以上

+2

另外'FILE * ecr(「result.txt」,「wt」);'無效。那裏需要有一個fopen。 – dunc123

+0

@ dunc123好點。讓我想知道OP如何得到這個代碼。 – stijn

+1

它確實會說「還有很多其他的事情需要解決」,但問題似乎只涉及到這一點。但如果這個問題得到解決,它可能會再次成爲另一個問題。 – doctorlove

3

如果你正面臨着幾大誤區有時候也值得看附近的人看看他們是否有幫助。

..._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 (
//   ^^ 

應該這樣做。我不敢運行它 - 它不是特例安全。 當你得到很多錯誤時不要驚慌 - 仔細閱讀它們

+0

+1只是最後一句話:] – stijn

2

你確定這是你正在編譯的代碼嗎?第50行是fclose(lec),它沒有提到fopen

其餘的是lec [53]的重新宣告,fopen [59]的缺席電話,一個「,」(逗號)應該是「;」 (分號)[82]和一個「(」應該是「[」[84]。