![]() |
| | #1 |
| Microsoft Lover Join Date: Nov 2003
Posts: 189
| 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;
}
I am so confused now |
| afreedboy is offline | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #3 | |
| Microsoft Lover Join Date: Nov 2003
Posts: 189
| Quote:
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 | |
| | #4 |
| and the hat of Jobseeking 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 |
| Salem is offline | |
| | #5 | |
| Microsoft Lover Join Date: Nov 2003
Posts: 189
| Quote:
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 | |
| afreedboy is offline | |
| | #6 |
| Microsoft Lover 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) |
| afreedboy is offline | |
| | #7 | |
| & the hat of GPL slaying Join Date: Sep 2001
Posts: 5,732
| Quote:
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 | |
| | #8 |
| Microsoft Lover 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 | |
| | #9 |
| & the hat of GPL slaying 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 | |
| | #10 |
| Microsoft Lover Join Date: Nov 2003
Posts: 189
| I am using gcc. Problem comes from main or gcc? |
| afreedboy is offline | |
| | #11 |
| and the hat of Jobseeking 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 |
| Salem is offline | |
| | #12 |
| & the hat of GPL slaying 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 | |
| | #13 |
| Microsoft Lover 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |