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.