-1

The following is a simple program that reads a .dat file that i included below. when i gcc compile from a windows command line everything works fine when i run the exe but when i run from a unix cmd line..... ".out" i get a segmentation fault. Why is this the case

#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!
Barmar
  • 741,623
  • 53
  • 500
  • 612
cmehmen
  • 249
  • 1
  • 3
  • 12
  • 4
    Which line is it getting the segmentation fault on? The debugger should tell you this. – Barmar Sep 09 '15 at 20:50
  • Segmentation faults generally occur when you use an uninitialized pointer or write outside the bounds of an array. Since you have no pointers (other than `fp`, which you use correctly), it must be the `word[]` array. Make sure you never access too far. – Barmar Sep 09 '15 at 20:51

3 Answers3

4

As well as the obvious upper limit, it seems that when count gets to 0, it can go negative.

if(count == 0){
    count-=1;
}

This will cause a seg fault with this

word[count] = ch;

Additionally you are using the dreaded feof

while(!feof(fp))

instead of

int ch;
while((ch = fgetc(fp)) != EOF)

Note the int type. Don't use char (or float) types unless you have a specific reason to. Library functions use int for character input and output, not char.

Community
  • 1
  • 1
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

It seems that when count gets to 15,then word[count]= ch; writes out of the bound of word

blakelead
  • 1,780
  • 2
  • 17
  • 28
0

I've just compile your file on my ubuntu 15.04, create lab1.dat file with your content and run it

vim a.c
vim lab1.dat
gcc a.c
./a.out
123456789101112131415p1data.dat
Mary 0123had 01a 0123456little 012344****,  0123its 012345678fleas
as 012345white 012as 012344****,  0123And 012345678910everywhere 01234****    01234**** 012344****,  0123456789the

My gcc -V output is

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.9.2-10ubuntu13' --with-bugurl=file:///usr/share/doc/gcc-4.9/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.9 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.9 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.9-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.9-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.9.2 (Ubuntu 4.9.2-10ubuntu13) 

So, program successful fun without any problem. Could you please specify linux distribution, gcc -V output and recheck if you have problem with your code.

BaZZiliO
  • 232
  • 1
  • 6