LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2012, 12:39 PM   #1
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Rep: Reputation: 12
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...
 
Old 01-30-2012, 12:46 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
pirate.txt is in the same directory as trun ?
 
Old 01-30-2012, 12:51 PM   #3
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@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?
 
Old 01-30-2012, 12:55 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Hu ? just type "ls" and enter to list files in the current directory
 
1 members found this post helpful.
Old 01-30-2012, 01:01 PM   #5
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@Cedrik
I moved pirate.txt. Pirate.txt is not in the same directory as trun.
 
Old 01-30-2012, 01:28 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
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
 
Old 01-30-2012, 01:43 PM   #7
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@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
 
Old 01-30-2012, 01:46 PM   #8
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Sorry

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
 
Old 01-30-2012, 01:54 PM   #9
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@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.
 
Old 01-30-2012, 06:05 PM   #10
ydoom
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Rep: Reputation: Disabled
I'm confused. Did the compilation succeed and generated a file named "trun" in your current directory?
 
Old 01-31-2012, 01:09 AM   #11
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@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!!??
 
Old 01-31-2012, 01:54 PM   #12
ydoom
LQ Newbie
 
Registered: Jan 2012
Posts: 11

Rep: Reputation: Disabled
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)                           = ?
 
Old 02-01-2012, 01:27 PM   #13
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scp truncate text file busy - copying file is not a running binary jetberrocal Linux - Server 3 06-24-2010 04:56 PM
text truncate swift2008 Programming 2 10-21-2008 08:18 PM
How do I include my own system call in file system driver kgp Linux - Kernel 3 06-06-2007 11:41 AM
truncate a text file in CLI? hottdogg Linux - General 2 09-06-2005 10:32 PM


All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration