PDA

View Full Version : linux prog problem



afreedboy
06-26-2004, 09:24 AM
#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 :confused:

Salem
06-26-2004, 09:40 AM
> 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.

afreedboy
06-26-2004, 09:55 AM
> 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.

:confused:


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

Salem
06-26-2004, 10:02 AM
It needs to be a char array!
Or you call malloc - your choice

Post your latest code

afreedboy
06-26-2004, 10:09 AM
It needs to be a char array!
Or you call malloc - your choice

Post your latest 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:



$ ./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
06-26-2004, 10:22 AM
i also tried debugging using gdb. here is what i got:



(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

:confused:

Thantos
06-26-2004, 10:44 AM
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:

#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;
}

afreedboy
06-28-2004, 12:13 AM
Mr. Thantos,

you just changed sizeof c right?

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

Thantos
06-28-2004, 12:21 AM
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?

afreedboy
06-28-2004, 02:19 AM
I am using gcc. Problem comes from main or gcc?

Salem
06-28-2004, 06:31 AM
Post your actual latest code - I've no idea whether you are running some modification of your code, or that provided by Thantos

Thantos
06-28-2004, 06:44 AM
well there could be a problem since the code is not C. Try it with g++ and see what happens.

afreedboy
06-30-2004, 08:31 AM
#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.