Windows和Linux(gcc)C编译器中的不同输出

时间:2017-10-22 00:19:35

标签: c linux windows gcc output

我有一个解决迷宫的程序,以便找到从开始(S)到退出(E)的可能路线。这是我的迷宫:

1111S11110
0000010001
110100010d
t001111110
0100000001
0111111101
1111111101
00000D01T1
0111110001
0000E01110

可能的路线是:

Start  S  W  W  S  S  S  E  E  E  E  E  E  S  S  S  S  W  W  N  W  W  W  W  W  W  S  S  E  E  E  E   Exit

这是正确的,我在CodeBlocks上得到它,但是当我通过putty在dev上编译我的代码时,我得到了这个:

Start  S  S  S  S  S  W  N  N  W  S  W  N  N  N  W  N   Exit

以下是我的全部代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


// Global Variables for use
int matrixSize,startX,startY,exitX,exitY;
char src[1500] = " ";
char Ndirection[50] = " N ";
char Sdirection[50] = " S ";
char Edirection[50] = " E ";
char Wdirection[50] = " W ";

// Function for finding the array length
int numOfLines(FILE *const mazeFile) {
  int c, count;
  count = 0;
  for (;; ) {
    c = fgetc(mazeFile);
    if (c == EOF)
      break;

    if (c == '\n')
      ++count;        // end of line => increment line counter
  }
  rewind(mazeFile);

  return count+1;
}

int capLetter(char ch){
   int result = 0;
   if(ch >= 'A' && ch <= 'Z'){
        result = 1;
   }
   return result;
}

int lowLetter(char ch){
   int result = 0;
   if(ch >= 'a' && ch <= 'z'){
        result = 1;
   }
   return result;
}

int isSafe(char Mazearray[matrixSize][matrixSize],int x,int y){
    if(x >= 0 && x < matrixSize && y >= 0 && y < matrixSize && Mazearray[x][y] != '1'){
        return 1;
    }
    return 0;


}

void MazeSolution(char Mazearray[matrixSize][matrixSize],int x,int y,char pathArray[matrixSize][matrixSize],char wasHereArray[matrixSize][matrixSize]){
    if(recursiveMaze(Mazearray,x,y,pathArray,wasHereArray) == 0){
        printf("There does not exist a possible solution!!!");
    }
    else{
        pathArray[startX][startY] = 'S';
    }
}

int recursiveMaze(char Mazearray[matrixSize][matrixSize],int x,int y,char pathArray[matrixSize][matrixSize],char wasHereArray[matrixSize][matrixSize]){
    if(x == startX && y == startY){
        pathArray[x][y] = 'S';
    }
    if(x == exitX && y == exitY){
        pathArray[x][y] = 'E';
        return 1;
    }
    // check if the coordinate is safe to go(not 1)
    if(isSafe(Mazearray,x,y) == 1 && wasHereArray[x][y] != '1'){
        wasHereArray[x][y] = '1';

        // Move North
        if(recursiveMaze(Mazearray,x-1,y,pathArray,wasHereArray) == 1){
            pathArray[x][y] = 'R';
            strcat(src,Ndirection);
            return 1;
        }
        // Move South
        if(recursiveMaze(Mazearray,x+1,y,pathArray,wasHereArray) == 1){
            pathArray[x][y] = 'R';
            strcat(src,Sdirection);
            return 1;
        }
        // Move East
        if(recursiveMaze(Mazearray,x,y+1,pathArray,wasHereArray) == 1){
            pathArray[x][y] = 'R';
            strcat(src,Edirection);
            return 1;
        }
        // Move West
        if(recursiveMaze(Mazearray,x,y-1,pathArray,wasHereArray) == 1){
            pathArray[x][y] = 'R';
            strcat(src,Wdirection);
            return 1;
        }
    }
    return 0;
}

// Main Function
int main( int argc, char **argv )
{
    // Opening the Matrix File
    FILE *mazeFile;
    mazeFile = fopen(argv[1], "r" );
    if( mazeFile == NULL )
        return 1;
    matrixSize = numOfLines( mazeFile );
    // Reading text file into 2D array
    int i,j;
    char mazeArray [matrixSize][matrixSize];

    for (i = 0; i<matrixSize; i++) {
        for (j = 0; j<matrixSize; j++) {
            fscanf(mazeFile, "%c", &mazeArray[i][j]);
        }

        char eol;
        fscanf(mazeFile, "%c", &eol);
    }
    // Variables

    //Creating path array
    char pathArray[matrixSize][matrixSize];
    for (i = 0; i < matrixSize; i++){
       for (j = 0; j < matrixSize; j++){
            pathArray[i][j] = '0';
        }
    }
    // CheckPoint array
    char wasHereArray[matrixSize][matrixSize];
    for (i = 0; i < matrixSize; i++){
       for (j = 0; j < matrixSize; j++){
            wasHereArray[i][j] = '0';
        }
    }
    // Finding start and exit indexes
    for (i = 0; i<matrixSize; i++) {
      for (j = 0; j<matrixSize; j++) {
        if(mazeArray[i][j] == 'S'){
            startX = i;
            startY = j;
        }
        if(mazeArray[i][j] == 'E'){
            exitX = i;
            exitY = j;
        }
      }
    }

    MazeSolution(mazeArray,startX,startY,pathArray,wasHereArray);
    char *data = src;
    int length=strlen(data);
    char bytes[length];
    int n=0;
    while(n<=length)
    {
       bytes[n] = data[length-n-1];
       n++;
    }

    FILE *f = fopen("path.txt", "w");
    fprintf(f, "Start %s Exit",bytes);
    fclose(mazeFile);
    fclose(f);
    return 0;
}

我不知道出了什么问题,从哪里开始?

1 个答案:

答案 0 :(得分:1)

DOS行结尾是CR-LF(“\ r \ n”),* nix行结尾只是LF(“\ n”)。更改这些行main

char eol;
fscanf(mazeFile, "%c", &eol);

为:

int c = fgetc(mazefile); // Slurp a '\r' carriage return or '\n' linefeed character.
if ('\r' == c) { 
    c = fgetc(mazefile); // slurp the '\n' linefeed character.
}