C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2004, 09:24 AM   #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
afreedboy is offline   Reply With Quote
Old 06-26-2004, 09:40 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,636
> 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.

Salem is offline   Reply With Quote
Old 06-26-2004, 09:55 AM   #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
afreedboy is offline   Reply With Quote
Old 06-26-2004, 10:02 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,636
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.

Salem is offline   Reply With Quote
Old 06-26-2004, 10:09 AM   #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.
afreedboy is offline   Reply With Quote
Old 06-26-2004, 10:22 AM   #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

afreedboy is offline   Reply With Quote
Old 06-26-2004, 10:44 AM   #7
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Quote:
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.
Thantos is offline   Reply With Quote
Old 06-28-2004, 12:13 AM   #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.
afreedboy is offline   Reply With Quote
Old 06-28-2004, 12:21 AM   #9
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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?
Thantos is offline   Reply With Quote
Old 06-28-2004, 02:19 AM   #10
Microsoft Lover
 
afreedboy's Avatar
 
Join Date: Nov 2003
Posts: 189
I am using gcc. Problem comes from main or gcc?
afreedboy is offline   Reply With Quote
Old 06-28-2004, 06:31 AM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,636
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.

Salem is offline   Reply With Quote
Old 06-28-2004, 06:44 AM   #12
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
well there could be a problem since the code is not C. Try it with g++ and see what happens.
Thantos is offline   Reply With Quote
Old 06-30-2004, 08:31 AM   #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.
afreedboy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:58 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22