LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2004, 11:15 PM   #1
cxel91a
Member
 
Registered: May 2001
Posts: 61

Rep: Reputation: 15
write function


I am having a problem getting this write function to work. All the parameters seem to be correct, but I get the following error message :
write error: Bad file descriptor

can someone tell me what is wrong here.

#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>

int main()
{

char buffer[80];
char data[80];
int fd;
long int BEGIN=0;

printf("Enter a name of the file to write to: ");
fgets(buffer,80,stdin);
buffer[strlen(buffer)-1]='\0';

if((fd=open(buffer,O_APPEND)) < 0){
perror("open error: ");
exit(1);
}
if((lseek(fd,BEGIN,SEEK_SET)) == BEGIN-1){
perror("lseek error ");
exit(2);
}
printf("Enter the data to insert into the file: ");
fgets(data,80,stdin);
data[strlen(data)-1]='\0';

if(write(fd,data,sizeof(data)) != sizeof(data)){
perror("write error ");
exit(3);
}

close(fd);

return 0;
}
 
Old 01-14-2004, 11:25 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
have you tried printf'ing the fd variable to see what it is?
 
Old 01-14-2004, 11:45 PM   #3
cxel91a
Member
 
Registered: May 2001
Posts: 61

Original Poster
Rep: Reputation: 15
It is the same file descriptor.
 
Old 01-15-2004, 08:00 AM   #4
fa3a
LQ Newbie
 
Registered: Aug 2003
Location: Minsk
Distribution: RHEL, Fedora
Posts: 8

Rep: Reputation: 0
I just compiled your code and got an error as follows:
"open error: : No such file or directory"

Thus I made a conclusion that O_APPEND should be applied for already existing files only. I've printed out the errno set after open call -- it is ENOENT. So you need to analyze the errno if open() call failed and call open() with O_CREAT flag when errno is ENOENT.

gl.
 
Old 01-15-2004, 11:09 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I believe you can specify O_CREAT in the initial open() call (i.e. open(buffer, O_APPEND | O_CREAT); ) and it will work.
 
  


Reply



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
C++ function to write text backwards? Artimus Programming 2 07-01-2005 09:18 AM
Write own stat() function... indian Programming 1 09-14-2004 07:06 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:21 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
Open Source Consulting | Domain Registration