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.
(caddr_t)-1 should be MAP_FAILED.
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] 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);
Last edited by Corona688; 12-10-2010 at 02:36 PM..
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..
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
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 ] .
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:
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)
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)
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)
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)
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)
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)
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)
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)