分段错误(适用于Windows但不适用于Unix)

时间:2015-09-09 20:45:25

标签: c segmentation-fault

以下是一个简单的程序,它读取下面包含的.dat文件。当我从一个Windows命令行编译gcc时,一切正常,当我运行exe但当我从unix cmd行运行.....“。out”我得到一个分段错误。为什么会这样呢

#include<stdio.h>

int main(){

    FILE *fp;
    char ch;
    char c;
    char word[15];
    int count = 0;
    int a;
    int boolean_comma = 0;

    fp = fopen("lab1.dat","r");

    if( fp == NULL) {
        printf("ERROR");
    }else
        while(!feof(fp)){

            ch = fgetc(fp);
            word[count] = ch;

            /*printf("%c",word[count]);*/

            if(ch == ','){

                count -= 1;
                boolean_comma = 1;
            }/*END IF*/

            if(ch == ' '){
                if(count == 0){
                    count-=1;
                }
                if(count == 4 && boolean_comma == 1){
                    printf("****, ");
                }

                if(count == 4 && boolean_comma == 0){

                    printf("**** ");
                }/*END IF*/

                else{

                    if(boolean_comma != 1){
                        for(a = 0; a < count; a++){

                            c = word[a];            
                            printf("%c",c);

                        }/*END FOR*/
                    }
                    boolean_comma = 0;
                    printf(" "); 
                    count = 0;
                }/*END ELSE*/

                count = 0;
                /*END IF*/}
            else{
                count++;
            }/*END ELSE*/

            printf("%i",count);
        }/*END WHILE*/

    fclose(fp);

    return 0;

}//end main

FILE

p1data.dat
Mary had a little lamb, its fleas
as white as snow, And everywhere that Mary went, the
fleas were sure to go!

0 个答案:

没有答案