Thread: linux prog problem

  1. #1
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189

    Unhappy linux prog problem

    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<errno.h>
    #include<string.h>
    #include<fcntl.h>
    int main(int argc, char *argv[])
    {        //  argv[1]="abc.txt";
            if(argc!=2)
            {
                    printf("Usage ./a.out <filename> \n");
                    return -1;
            }
            char filename[20];
            strcpy(filename, argv[1]);
            int fd;
            fd=open(filename,O_RDONLY);
            if(fd<0)
            {
            printf("error open file");
            return -1;
            }
            int b=lseek(fd,0,SEEK_SET);
            if(b!=0)
            {printf("internal file error");
            return -1;
            }
    
            char *c;
            int d=1;
            while(d!=0)
            {
            int i=0;
            d=read(fd,c,22);
            if(d==-1)
            {printf("file error");int errno;printf("%s",errno);return -1;}
            if(d!=0)
            while(d>i)
            {
    
            printf("%c",c[i]);
            i++;
            }
            }
            close(fd);
            return 0;
    }
    That is my program that I write with Turbo C. And then I run that program in my SuSe 9. That works absolutely fine except I need to change some #include files. That is not a problem. But when I connect to Unix server by using secure CRT and run on it. It gave me error from I got by errno:Segmentation fault

    I am so confused now

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char *c;
    Which 22 bytes is this pointing at?

    Try
    char c[22];

    Oh, and are you writing this in C or C++
    Because it's a lot like neither at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Quote Originally Posted by Salem
    > char *c;
    Which 22 bytes is this pointing at?
    hey I change it to char *c and now it is working.
    when i type something like " ./a.out abc.txt "

    it print out all text from abc.txt, but still print segmentation fault.

    i already turn off errno lines.




    by the way, i intend this program as a C program

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It needs to be a char array!
    Or you call malloc - your choice

    Post your latest code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Quote Originally Posted by Salem
    It needs to be a char array!
    Or you call malloc - your choice

    Post your latest code
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    //#include<errno.h>
    #include<string.h>
    #include<fcntl.h>
    int main(int argc, char *argv[])
    {        //  argv[1]="abc.txt";
            if(argc!=2)
            {
                    printf("Usage ./a.out <filename> \n");
                    return -1;
            }
            char *filename;
            strcpy(filename, argv[1]);
            int fd;
            fd=open(filename,O_RDONLY);
            if(fd<0)
            {
            printf("error open file");
            return -1;
            }
            int b=lseek(fd,0,SEEK_SET);
            if(b!=0)
            {printf("internal file error");
            return -1;
            }
    
            char c[2];
            int d=1;
            while(d!=0)
            {
            int i=0;
            d=read(fd,c,1);
            if(d==-1)
            {
            printf("file error");
    //      int errno;
    //      printf("%s",errno);
            return -1;
            }
            if(d!=0)
            while(d>i)
            {
    
            printf("%c",c[i]);
            i++;
            }
            }
            close(fd);
            return 0;
    }

    that is my latest code and here is output:

    Code:
    $ ./a.out abcd.txt
    1234567
    Segmentation fault
    in abcd.txt i already put 1234567, so i want only 1234567. i don't know where Segmentation fault come from though.

  6. #6
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    i also tried debugging using gdb. here is what i got:

    Code:
    (gdb) step
    43              if(d!=0)
    (gdb) step
    32              while(d!=0)
    (gdb) step
    51              close(fd);
    (gdb) step
    52              return 0;
    (gdb) step
    53      }
    (gdb) step
    0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
    (gdb) step
    Single stepping until exit from function __libc_start_main, 
    which has no line number information.
    
    Program received signal SIGSEGV, Segmentation fault.
    0x4000c6a0 in _dl_fini () from /lib/ld-linux.so.2
    (gdb)
    i don't know which part of my program transmit SIGSEGV


  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    by the way, i intend this program as a C program
    This is most definatly NOT a C program. Also fix your indentation

    I ran it fine. Made a few changes but nothing major:
    Code:
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<unistd.h>
    #include<string.h>
    #include<fcntl.h>
    
    int main(int argc, char *argv[])
    {
            if(argc!=2)
            {
                    printf("Usage ./a.out <filename> \n");
                    return -1;
            }
    
            int fd;
            fd=open(argv[1],O_RDONLY);
            if(fd<0)
            {
              printf("error open file");
              return -1;
            }
            // This part really isn't needed as it will already be pointed at the beginning
            int b=lseek(fd,0,SEEK_SET);
            if(b!=0)
            {
              printf("internal file error");
              return -1;
            }
    
            char c[2];
            int d=1;
            while(d!=0)
            {
              int i=0;
              d=read(fd,c,sizeof c);
              if(d==-1)
              {
                printf("file error");
                return -1;
              }
              if(d!=0)
                while(d>i)
                {
                  printf("%c",c[i]);
                  i++;
                }
            }
    
            close(fd);
            return 0;
    }
    Last edited by Thantos; 06-26-2004 at 10:52 AM.

  8. #8
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Mr. Thantos,

    you just changed sizeof c right?

    That still don't work. I mean I can run but still show segmentation fault.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    looking at the gdb log you posted it appears the fault is happening after main() terminates.

    Are you using gcc or g++? If so which version?

  10. #10
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    I am using gcc. Problem comes from main or gcc?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your actual latest code - I've no idea whether you are running some modification of your code, or that provided by Thantos
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well there could be a problem since the code is not C. Try it with g++ and see what happens.

  13. #13
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189
    Code:
    #include<sys/stat.h>
    #include<unistd.h>
    //#include<string.h>
    #include<fcntl.h>
    
    int main(int argc, char *argv[])
    {
    
            int fd;
            char c;
            int d=1;
            int i=0;
    
            if(argc!=2)
            {
                    printf("Usage ./a.out <filename> \n");
                    return -1;
            }
    
            fd=open(argv[1],O_RDONLY);
            if(fd<0)
            {
              printf("error open file");
              return -1;
            }
    
            while(d!=0)
            {
              d=read(fd,&c,1);
              if(d==-1)
              {
                    printf("file error");
                    return -1;
              }
              printf("%c",c);
            }
            close(fd);
            return 0;
    }


    One of my friend rewrite like above and that is working fine on that Unix server. But I can't find any clues why his server is refusing first code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux Problem Screen Resoultion
    By Matus in forum Tech Board
    Replies: 1
    Last Post: 01-29-2009, 04:39 PM
  2. Problem Installing Wireless Drivers In Linux
    By mike_g in forum Tech Board
    Replies: 1
    Last Post: 12-05-2007, 05:53 AM
  3. C99 standard prog buggy with Linux but not Windows
    By Greenman in forum C Programming
    Replies: 15
    Last Post: 01-06-2004, 04:54 PM
  4. Modem problem w/ RedHat Linux 9
    By Machewy in forum Tech Board
    Replies: 10
    Last Post: 01-05-2004, 06:58 PM
  5. Linux modem problem
    By VooDoo in forum Linux Programming
    Replies: 5
    Last Post: 08-19-2002, 05:34 AM