No error but not executing


 
Thread Tools Search this Thread
Top Forums Programming No error but not executing
# 1  
Old 12-10-2010
No error but not executing

Hi friends
When I compile thic program by gcc filename, it shows no error.
But after that if I execute the program gets stuck.
Can any one find out.






Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n";
size_t length=strlen(the_file);
size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==(caddr_t)-1)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<length;++i)
{
if(*(the_file+i)>='0' && *(the_file+i)<='9')
*(the_file+i)='*';
sleep(1);
}
printf("Parent done with changes\n");
 printf("The file now contains:\n%s\n",the_file);
exit(0);
}

---------- Post updated at 10:59 AM ---------- Previous update was at 10:47 AM ----------

That program has errors ... I changed it and removed all errors.
Now, there is executing error. Check properly

---------- Post updated at 11:01 AM ---------- Previous update was at 10:59 AM ----------

sorry .. it did not show up.. thats y i posted it again

Last edited by pludi; 12-10-2010 at 11:56 AM.. Reason: code tags, please...
# 2  
Old 12-10-2010
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
        int fd,changes,i,random_spot,kids[2];
        struct stat buf;
        char *the_file,*starting_string="Finally got it! \n";
        size_t length=strlen(the_file);

strlen(the_file) will either crash or return garbage when you give it a variable you didn't initialize. the_file could contain anything at that point.

You don't ever set the_file to a string anyway, so strlen() will still return garbage or crash even after you initialize it. Remember, not all arrays are strings! Your file will contain:

Finally got it! \n while a string contains Finally got it! \n\0 The \0 is how strlen() knows where the string ends, the compiler adds it to the end of things in double-quotes for you. strlen() doesn't count it in the length either, so you never write it.

You don't need length anyway. buf.st_size already tells you the exact size of the file.

Code:
size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==(caddr_t)-1)

(caddr_t)-1 should be MAP_FAILED.
Code:
{
fprintf(stderr,"mmap failure\n");
exit(5);
}

for(i=0;i<length;++i)
{
if(*(the_file+i)>='0' && *(the_file+i)<='9')
*(the_file+i)='*';
sleep(1);
}

replace 'length' with buf.st_size. strlen() can't tell you how long a file is, but fstat already did.

*(the_file+i) can be written much more clearly as the_file[i]
Code:
printf("Parent done with changes\n");
 printf("The file now contains:\n%s\n",the_file);

the_file is still not a string. You can't print it with %s because printf won't know where the text ends. You do know the file size, though, which will let you write it instead. write(STDOUT_FILENO, the_file, buf.st_size);
Code:
exit(0);
}


Last edited by Corona688; 12-10-2010 at 02:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-10-2010
But the program shows lot of errors now.
Can you try it????

Thank you very much
# 4  
Old 12-10-2010
Quote:
Originally Posted by gokult
But the program shows lot of errors now.
What errors? In what code? I can't see the changes you've made to your program from here.
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n";
size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<buf.st_size;++i)
{
if(the_file[i]>='0' && the_file[i]<='9')
the_file[i]='*';
// the program takes seventeen seconds to run with this in
// sleep(1);
}
printf("Parent done with changes\n");
 printf("The file now contains:\n");
write(STDOUT_FILENO, the_file, buf.st_size);
printf("\n");
exit(0);
}

# 5  
Old 12-10-2010
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n\0";
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);

}
write(fd,starting_string,strlen(starting_string));
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<strlen(the_file);++i)
{
if(*(the_file[i])>='0' && *(the_file[i])<='9')
*(the_file[i])='*';
sleep(1);
}
printf("Parent done with changes\n");
write(STDOUT_FILENO, the_file, buf.st_size);
exit(0);
}

ERRORS:

num.c: In function `main':
num.c:33: warning: comparison between pointer and integer
num.c:40: invalid type argument of `unary *'
num.c:40: invalid type argument of `unary *'
num.c:41: invalid type argument of `unary *'

please use code tags. Thank you

---------- Post updated at 04:32 PM ---------- Previous update was at 04:00 PM ----------

code tag means???

Last edited by jim mcnamara; 12-10-2010 at 05:24 PM..
# 6  
Old 12-10-2010
Code:
char *the_file,*starting_string="Finally got it! \n\0";

As I said, strings in quotes already have a null terminator. The compiler adds them for you. So putting a \0 here is pointless, it already has one, it's just not being written to the file since strlen() doesn't count the NULL terminator as part of the string.

Which is fine, I think. You should be checking the length of the file, not just blindly reading memory. If you go beyond the end of the file in mmap-ed memory, that's either a segfault or a bus error -- a crash.

So stop using string functions already and just treat the_file like an array of characters.
Quote:
for(i=0;i<strlen(the_file);++i)
I repeat: strlen() only works for strings. the_file is not a string. use buf.st_size
Code:
{
if(*(the_file[i])>='0' && *(the_file[i])<='9')
*(the_file[i])='*';
sleep(1);
}

Instead of using my syntax or yours, you're trying to use both. either use the_file[i], or *(the_file + i). Note the lack of * in one of the two.
Quote:
code tag means???
Tags that surround code to make your code readable instead of a mess. If you don't understand how they work, hit the 'quote' button for my post here, I've used lots of them. [ code ] stuff that is code [ /code ] without the extra spaces between [ and ] .
# 7  
Old 12-11-2010
I never call write() but I error check right there, check amount written, and loop if EINTR, EAGAIN if O_NONBLOCK, too, all part of the joys of raw I/O:
Code:
char *write_point ;
int char_to_write ;
int ret ;

for ( char_to_write = strlen( starting_string ), write_point = starting_string ; char_to_write ; /* nothing */ ){
  if ( 0 > ( ret = write( fd, write_point, char_to_write )){
    if ( errno == EINTR )
      continue ;
     if ( errno = EAGAIN ){
       poll( 0, 0, 1 ); /* sleep a millisecond */
       continue ;
      }
     perror( "write error" );
     exit( 1 );
   }
  write_point += ret ;
  char_to_write -= ret ;
 }

This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Error when executing script

Hi, I wrote this script to test if the output for DIR1 and DIR2 comes out as I want : #!/bin/bash DAY=$(date +%d) MONTH=$(date +%b) YEAR=$(date +%Y) DIR1=$($MONTH$YEAR"_Blast_BC01") DIR2=$($MONTH$YEAR"_Blast_BC15") echo $DIR1 echo $DIR2 This is the output I want for echo $DIR1 ... (12 Replies)
Discussion started by: anaigini45
12 Replies

2. Programming

Error in executing the C program

Hello Friends, I have written a code for the unisex bathroom which makes a policy that when a woman is in the bathroom only other women may enter, but not men, and vice versa. This program consists of four functions which a user defines but these functions are not properly working while... (4 Replies)
Discussion started by: Ravi Tej
4 Replies

3. UNIX for Dummies Questions & Answers

Error executing the script

I have the following script test.sh owned by dwdev account and group dwdev, the permissions on the script are as follows. -rw-r-x--- 1 dwdev dwdev 279 Sep 17 13:19 test.sh Groups: cat /etc/group | grep dwdev dwdev:x:704:dwdev dwgroup:x:725:dwdev writers:x:726:dwdev User: cat /etc/passwd |... (3 Replies)
Discussion started by: Ariean
3 Replies

4. Linux

Error executing a variable

greetings, i'll try to keep this simple... i have a script that sets up my environment and creates a command line variable to execute. when i execute the variable i get an error telling me it cannot open one of the files on the command line. the error prints a file name that is definitely... (7 Replies)
Discussion started by: crimso
7 Replies

5. Shell Programming and Scripting

Error while executing sh command

Hi, I have 2 files temp1.sh and temp2.sh as follows: =========== temp1.sh =========== echo "session1" sh temp2.sh echo "exit session2 and enter session1" ================================= ============= temp2.sh ============= echo "session2" sh echo "exit session2"... (5 Replies)
Discussion started by: RP09
5 Replies

6. Shell Programming and Scripting

Error executing script

Please delete de thread. Thanks. (10 Replies)
Discussion started by: Rodrih92
10 Replies

7. Programming

No error but not executing

Hi friends NO errors, but when I try to execute the program it gets struck. Can any one find it out. #include<stdio.h> #include<sys/types.h> #include<sys/mman.h> #include<stdlib.h> #include<fcntl.h> #include<sys/stat.h> #include<unistd.h> #include<signal.h> #include<string.h> ... (0 Replies)
Discussion started by: gokult
0 Replies

8. UNIX for Dummies Questions & Answers

Error Executing the script.

Hi , I m getting an error after executing the script. My script. Script is used to find out the date on 8 different machines(mentioned in SERVERNAMES file). I have added public key to avoid ssh password and ssh without password working fine. #!/bin/sh fn_VMFind() { Date=`ssh -t... (5 Replies)
Discussion started by: pinga123
5 Replies

9. Shell Programming and Scripting

Error while executing a script

My script is as below : #!/bin/sh for line in `cat Results.txt` do FEILD1=`echo $line |awk -F"|" '{print $1}'` FEILD2=`echo $line |awk -F"|" '{print $2}'` FEILD3=`echo $line |awk -F"|" '{print $3}'` FEILD4=`echo $line |awk -F"|" '{print $4}'` echo "$FEILD1 $FIELD2 $FIELD3 $FIELD4" done ... (15 Replies)
Discussion started by: shwetainnani
15 Replies

10. Shell Programming and Scripting

error while executing the script

Hello I am executing the following script nawk 'NR == 1 || substr($0,63,5) ~ /H... / && \ _++ == 2 { fn && close(fn); fn = "part_" ++c; _ = 1 } { print > fn }' sample.dat When i execute as it is it is executing fine. but when i execute the whole script as a single line like below ... (2 Replies)
Discussion started by: dsdev_123
2 Replies
Login or Register to Ask a Question

Featured Tech Videos