LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-26-2012, 01:09 PM   #1
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Rep: Reputation: 12
Open a file from a c program using system call.


Hi everyone,
I am trying to open a simple file using system call open(). The code I am using is
Code:
#include<sys/types.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
int open(const char *name, int O_WRONLY);
int main(void)
{
	int fd;
	fd=open("/usr/bin/file", O_WRONLY);
	if(fd==-1)
		fprintf(stdout, "Error\n");
	return(0);
}
I am compiling using
Code:
$ gcc filename.c -o filename
The error is
Code:
open.c:5: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
Kindly help. Thanks.
Regards...
 
Old 01-26-2012, 01:18 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
open is declared in fcntl.h, you shouldn't be re-declaring it; just use it directly.
 
Old 01-27-2012, 05:49 AM   #3
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by jhwilliams View Post
open is declared in fcntl.h, you shouldn't be re-declaring it; just use it directly.
You can just remove redundant declaration.

The problem is O_WRONLY is define so 'int O_WRONLY' expanded by preprocessor to something like 'int 2'
 
Old 01-27-2012, 05:51 AM   #4
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
Thank you.

I got rid of the error but replaced by another annoying one which i have encountered a lot many times. I modified the code like you said
Code:
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>

int main(void)
{
	int fd;
	fd=open("/home/abhishek/downloads/linux.pdf", O_RDONLY);
	if(fd==-1)
		/*error*/
		
	return(0);
}
The error again
Code:
open2.c: In function ‘main’:
open2.c:14: warning: control reaches end of non-void function
I am grateful for your help. Thanks...
 
Old 01-27-2012, 06:30 AM   #5
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@jhwilliams
I got rid of the last error too! Now the code is error free, but the pathname i mentioned in open() isn't opening in a new window. The value of the variable fd(file descriptor) is now 3. Am i correct so far? Thanks.
 
Old 01-27-2012, 02:57 PM   #6
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by abhishekgit View Post
@jhwilliams
I got rid of the last error too! Now the code is error free, but the pathname i mentioned in open() isn't opening in a new window. The value of the variable fd(file descriptor) is now 3. Am i correct so far? Thanks.
open() is used to start access to a file, for use with read() or write().

If you want to display the file in a graphical user interface, you could try just using evince, from the command line, instead of writing a C program.

Code:
 evince file.pdf
 
Old 01-27-2012, 07:22 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
To further clarify what jhwilliams said, the open() function simply creates a file descriptor (which your code assigns to fd). Subsequent to that, you can perform various operations on the file, such as reading, writing (but in this case not: O_RDONLY ), seeking, closing, etc. In each case, the file descriptor is used to identify the open file upon which your code is to operate. You will not visibly see anything to indicate that a file is open.

There is no windowing associated with the open() call on a file. Indeed, windowing is not part of the C programming language, per se. Windowing and other GUI oriented actions are done using functions compiled into special libraries for that purpose. There are numerous categories of such libraries, depending on such things as the underlying graphical subsystem (such as X), the OS (Windows vs Linux/X), etc. Most windowing systems are therefore quite non-portable across hardware and OS architectures.

Opening of files using the open() system call in C is, by contrast, quite portable. It is a fairly sure bet that any architecture that has some kind of filesystem and a C compiler will use the open() call to open files for reading or writing.

--- rod.
 
Old 01-27-2012, 10:07 PM   #8
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@jhwilliams
Thank you. I got that. I was looking for the file to be opened in gui. evince works like a charm though. :-)
 
Old 01-27-2012, 10:10 PM   #9
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@theNbomr Thank you. Got it! :-)
 
  


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
[SOLVED] System Call from Cobol Program to execute a *.bat file with a parameter? michellestew AIX 2 12-29-2010 08:31 AM
A question about the system call mount in a C program dariyoosh Programming 2 08-16-2009 05:41 PM
issue system call from running fortran program msander Linux - Newbie 1 04-24-2009 07:49 AM
HELP:Error running program which includes system call stefio Programming 2 07-27-2008 10:37 AM
How to issue system call from running fortran program swerdna Programming 3 12-27-2006 07:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:55 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