LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   To truncate a text file using system call (https://www.linuxquestions.org/questions/linux-newbie-8/to-truncate-a-text-file-using-system-call-926542/)

abhishekgit 01-30-2012 11:39 AM

To truncate a text file using system call
 
Hello people,
I am studying a small piece of code to truncate a text file using system call. This is the code.
Code:

#include<stdio.h>
#include<unistd.h>

int main()
{
        int ret;
        ret = truncate("./pirate.txt", 45);
        if(ret==-1)
        {
                perror("truncate");
                return -1;
        }
        return 0;
}

where "pirate.txt" is the existing text file. I execute it through gcc using
Code:

$ gcc trun.c -o trun
$ ./trun

The error is
Code:

bash: ./truncate: No such file or directory
Kindly help. Thank you.

Regards...

Cedrik 01-30-2012 11:46 AM

pirate.txt is in the same directory as trun ?

abhishekgit 01-30-2012 11:51 AM

@Cedrik
I have edited a pirate.txt file as an example. I do not know its location. How to find out whether they are the same directory?

Cedrik 01-30-2012 11:55 AM

Hu ? just type "ls" and enter to list files in the current directory

abhishekgit 01-30-2012 12:01 PM

@Cedrik
I moved pirate.txt. Pirate.txt is not in the same directory as trun.

Cedrik 01-30-2012 12:28 PM

But it has to be in the same dir as trun
"./pirate.txt" => relative path that means file pirate.txt is in the current directory

abhishekgit 01-30-2012 12:43 PM

@Cedrik
I moved pirate.txt again, Now when i run ls command, pirate.txt as well as trun.c are listed in the current directory.
I recompiled and still not able to. Any other alternative? Thanks

Cedrik 01-30-2012 12:46 PM

Sorry :o

I did not read the error carefully
Code:

bash: ./truncate: No such file or directory
You tried to execute truncate but you named your program "trun"

Try:
Code:

./trun

abhishekgit 01-30-2012 12:54 PM

@Cedrik
Oh no I am sorry too. I wrote two identical programs just to clarify. Truncate.c and Trun.c and misplaced the error code.
I have Executed ./trun too. I am getting the exact same error. :o

ydoom 01-30-2012 05:05 PM

I'm confused. Did the compilation succeed and generated a file named "trun" in your current directory?

abhishekgit 01-31-2012 12:09 AM

@ydoom
Compilation is successful, i.e I am getting no errors or warnings after I run $ gcc trun.c -o -trun. After i run ./trun bash says No such file or directory!!?? :Pengy:

ydoom 01-31-2012 12:54 PM

Just a side note, I hope: if you run
Code:

gcc trun.c -o -trun
this generates file "-trun" (minus sign in front).

But let's assume that the file "trun" exists :) Try running "strace trun", this return (for me), when there's no file "pirate.txt":
Code:

execve("./trun", ["trun"], [/* 60 vars */]) = 0
brk(0)                                  = 0x804a000
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77d4000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=181371, ...}) = 0
mmap2(NULL, 181371, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb77a7000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\20o\1\0004\0\0\0"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1651695, ...}) = 0
mmap2(NULL, 1452456, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7644000
mprotect(0xb77a0000, 4096, PROT_NONE)  = 0
mmap2(0xb77a1000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15c) = 0xb77a1000
mmap2(0xb77a4000, 10664, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb77a4000
close(3)                                = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7643000
set_thread_area({entry_number:-1 -> 6, base_addr:0xb76436c0, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
mprotect(0xb77a1000, 8192, PROT_READ)  = 0
mprotect(0xb77f2000, 4096, PROT_READ)  = 0
munmap(0xb77a7000, 181371)              = 0
truncate("./pirate.txt", 45)            = -1 ENOENT (No such file or directory)
dup(2)                                  = 3
fcntl64(3, F_GETFL)                    = 0x8001 (flags O_WRONLY|O_LARGEFILE)
close(3)                                = 0
write(2, "truncate: No such file or direct"..., 36truncate: No such file or directory) = 36
exit_group(-1)                          = ?

The 6th line from bottom is the failure point. After executing "touch pirate.txt" (creating that file), the output finishes with
Code:

truncate("./pirate.txt", 45)            = 0
exit_group(0)                          = ?


abhishekgit 02-01-2012 12:27 PM

Thank you.
 
@ydoom
That was the mistake I was making!! I was executing -trun all the time. i just ran $gcc trun.c -o trun, which perfectly worked!!. I appreciate your help. Thanks for your time.


All times are GMT -5. The time now is 03:27 AM.