open() system call in c++????


 
Thread Tools Search this Thread
Top Forums Programming open() system call in c++????
# 1  
Old 04-12-2012
open() system call in c++????

Hi friends,
I am trying to use the open system call in c++ language. Please have a look at my code.
vi Open.cpp
Code:
 1 #include <stdio.h>
      2 #include <iostream>
      3
      4 #define BUFSIZE 1
      5
      6 using namespace std;
      7
      8 int main()
      9 {
     10         int f1, n;
     11         char buf[BUFSIZE];
     12         int i = 0;
     13         f1 = open("typing.txt", 0);
     14
     15         while((n = read(f1, buf, BUFSIZE)) > 0)
     16         {
     17                 cout << buf[0];
     18         }
     19 return 0;
     20 }

The g++ compiler gives the following error,

Code:
Open.cpp: In function âint main()â:
Open.cpp:13:27: error: âopenâ was not declared in this scope

please help me.
Thanks!
# 2  
Old 04-12-2012
NAME
open, creat - open and possibly create a file or device

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


From the manual, it seems you'd need to include some more headers. open() is the low-level call. fopen() is part of stdio.h. If you'd like to use fopen() you'd use fread(). It's a little different.

Last edited by neutronscott; 04-12-2012 at 03:17 PM.. Reason: info about fread()
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 04-12-2012
The manual page should be available on your system via 'man 2 open'. 2 is the man section for system calls.
# 4  
Old 04-12-2012
I think, you need to include this library #include <unistd.h>, its include many funtions to use under Unix. Thanks!!
# 5  
Old 04-13-2012
This is an important difference between the C and the C++ languages. In C++, declaration of function prototypes are mandatory. In C, they are recommended, but not mandatory.
# 6  
Old 04-13-2012
In C, if you don't declare them, they may crash, so that's pretty "mandatory". But the compiler will only warn you.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

system call

Trying to figure out a load issue with a webserver. I have traced a php script and noticed the following connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("XX.XX.XX.XX")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000035> poll(, 1, 2000) = 1 () <0.000120>... (5 Replies)
Discussion started by: rajan007
5 Replies

2. Programming

need help with system call

hi everyone i wrote a system call and compiled the kernel succesfully... my system call is in a file in the kernel folder named my_syscall1.c (kernel/my_syscall1.c) the header file for this system call i added it in the folder include like this include/my_syscall1/my_syscall1.h my problem is... (2 Replies)
Discussion started by: demis87
2 Replies

3. Programming

system call

I have a cgi script which is called after certain time interval, which has this: system ("ls -l /tmp/cgic* | grep -v \"cgicsave.env\" | awk '{print $5}'"); During the execution of this script,the output is 0 sometimes. But due to this the system call is not working at all and doesnt o/p... (2 Replies)
Discussion started by: xs2punit
2 Replies

4. Programming

Is there a system call other than 'open' for opening very large files?

Dear all, Inside a C program, I want to open a very big file (about 12 GB) in order to read its content. Here is the code: /* argv contains the path to the file. */ inputFileDescriptor = open(argv, O_RDONLY); if (inputFileDescriptor < 0) { ... (6 Replies)
Discussion started by: dariyoosh
6 Replies

5. UNIX for Advanced & Expert Users

System Call Wrapper of 'open'

When the programmer use 'open' function, the process is like below. "open -> system call wrapper of open in Glibc -> syscall_open in kernel" I found the wrapper of open, but there is no implementation like 'int $80'. int __open (file, oflag) const char *file; int oflag; { ... (3 Replies)
Discussion started by: yuno96
3 Replies

6. Programming

C:system call

Hi I'm studing the system call. I've written a small program that return the time spent in doing some operations. Now I'd like to write one that return the time spent in user mode of a process. I'm reading that i should use the tms struct: clock_t times(struct tms *buf); struct tms {... (2 Replies)
Discussion started by: Dedalus
2 Replies

7. Shell Programming and Scripting

system call

Hi, How to write a system calls in a script ? > cd $HOME > ls -ltr thanks in advance.. (10 Replies)
Discussion started by: hegdeshashi
10 Replies

8. Programming

c system call

How the c compiler differentiates the system calls and function calls? (1 Reply)
Discussion started by: rangaswamy
1 Replies

9. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies
Login or Register to Ask a Question