2012-03-27 115 views
0

它再次與我這個項目。即時通訊有問題閱讀我的輸入文本導致程序永遠不會結束。這裏的問題full description of the problem 但我不知道如何通過字符讀取文本char,然後以相同的方式讀取下一行。如何通過字符和C++逐行閱讀文本字符?

freopen("input.txt","rt",stdin); 
freopen("output.txt","wt",stdout); 

int n=-1; 
int m=-1; 
int cont =0; 
int VMatriz[100][100]={0}; 
int Mapa[100][100]={0}; 
while(n!=0 && m!=0) 
{ 
    scanf("%d %d",&n,&m); 

    if (n==0 && m==0) 
     break; 
    cont++; 
    printf("Field # %d",cont); 


    for (int i=0;i<n;i++) 
    { printf("/n"); 
     for (int j=0;j<m;j++) 
     { 
      //scanf("%d ",&Mapa[i][j]); 
      Mapa[i][j]=getchar(); 

      if (Mapa[i][j]=='*') 
       { 
        if (j-1>=0) 
         VMatriz[i][j-1]++; 
        if (j+1<m) 
         VMatriz[i][j+1]++; 
        if (i-1>=0) 
         VMatriz[i-1][j]++; 
        if (i+1<m) 
         VMatriz[i+1][j]++; 

        if (j-1>=0 && i-1>0) 
         VMatriz[i-1][j-1]++; 
        if (j-1>=0 && i+1<m) 
         VMatriz[i+1][j-1]++; 
        if (j+1<m && i-1>0) 
         VMatriz[i-1][j+1]++; 
        if (j+1<m && i+1<m) 
         VMatriz[i+1][j+1]++; 

        VMatriz[i][j]='*'; 

       printf("%d",VMatriz[i][j]); 


       } 

     } 

    } 
    printf("/n"); 


} 
return 0; 

回答

0

首先使邏輯簡單

  1. 讀一行行,直到你到達EOF
  2. 解析線爲每個字符

您可以使用CStdioFile

CStdioFile file (_T("File.txt"), CStdioFile::modeRead); 
CString buffer; 

while (file.ReadString(buffer)) 
{ 
    CString strLine = buffer; 
    //Parse the strLine for each character 
}