LinuxQuestions.org
Visit Jeremy's Blog.
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 11-03-2007, 03:22 PM   #1
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Rep: Reputation: 30
simple c linux programing question


I just don't get why I can't write to the file I create using open with the filedescriptor I received ?

Code:
#include <stdio.h>
#include <unistd.h> // write syscall
#include <fcntl.h> // Open syscall
#include <sys/types.h>
#include <sys/stat.h>
int main ()
{
	const char *strPath = "/path/to/test.check";
	int nfd = open(strPath, O_CREAT | O_APPEND, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
	ssize_t ssWriteResponse = write(nfd,"Hello World",12);
	int nCloseRV = close(nfd);
	return 0;
}


File is being created, but the write doesn't work...

Thanks for your help.
 
Old 11-03-2007, 03:57 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
You dont dident open the file for writing. Also, "Hellow World" is only 11 characters.

Code:
{
	const char *strPath = "Mytest";
	int nfd = open(strPath, O_CREAT | O_APPEND | O_RDWR, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
	write(nfd,"Hello World",11);
	close(nfd);
	return 0;
}
 
Old 11-03-2007, 04:59 PM   #3
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by SciYro View Post
You dont dident open the file for writing. Also, "Hellow World" is only 11 characters.

Code:
{
	const char *strPath = "Mytest";
	int nfd = open(strPath, O_CREAT | O_APPEND | O_RDWR, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
	write(nfd,"Hello World",11);
	close(nfd);
	return 0;
}
THanks DUDE!
Havn't noticed that..

also, about the 12, yeah, I've noticed it just now, anyways what I'm really doing is write(nfd,strWriteToFile,strlen(strWriteToFile)); so I don't have to recount every time the string is changed.


Thanks!
 
Old 12-23-2007, 03:17 PM   #4
itz2000
Member
 
Registered: Jul 2005
Distribution: Fedora fc4, fc7, Mandrake 10.1, mandriva06, suse 9.1, Slackware 10.2, 11.0, 12.0,1,2 (Current)]
Posts: 732

Original Poster
Rep: Reputation: 30
sorry for being lame, but can u see the problem here?

Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#endif

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	
	int fd = open("/home/zuki/script.ini",O_RDONLY);
	char *buf;
	buf = "";
	size_t rval;
	size_t wval;
       	while ((rval = read(fd, &buf, 14)) != EOF)
			{
				if ((wval = write(1, buf, rval))!=rval)
				{
					char *buf = "An error has occured, returning";
					write(2,buf,strlen(buf));
					return(1);
				}
			}

	return(0);
}
THANKS
 
Old 12-23-2007, 04:45 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
There's no storage associated with buf; it's merely a value that currently denotes a null string literal. There's no place for the read data to be placed. You either need to make buf a char[14] or use malloc.
ta0kira
 
  


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
How to monitor a device? A Linux Programing question. randomsel Programming 4 09-26-2007 10:34 PM
Newbie programing question matthewa Programming 4 03-20-2005 04:37 PM
C programing question israel Programming 2 11-29-2004 06:05 AM
Simple programing question neozero62 Linux - Newbie 5 10-30-2003 09:33 PM

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

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

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