Sponsored Content
Homework and Emergencies Homework & Coursework Questions Using C language to use system calls Post 302734115 by Corona688 on Wednesday 21st of November 2012 03:53:45 PM
Old 11-21-2012
It's possible to do it either way. Line-by-line is probably easier.

Your using scanf incorrectly every time you use it. You can't store a string in a char, since a string is a char array, and you shouldn't be using scanf to read raw data. Read lines then feed them into sscanf, or your code may get stuck in infinite loops whenever the user types something wrong.

I'd do this:

Code:
while(1)
{
        char buf[512];
        int selection;
        if(fgets(buf, 512, stdin) == NULL) break; // Read line, quit on error
        // Convert line into number
        if(sscanf(buf, "%d", &selection) != 1)
        {
                fprintf(stderr, "you did not type a number\n");
                continue;
        }

        switch(selection)
        {
...
        }
}

Also, you cannot store an entire string inside a single character. You'll need to feed sscanf an array for the filenames. Something like this:

Code:
char fname[512];
printf("Please enter a filename:\n");
// Read a line
fgets(fname, 512, stdin);
// chop the newline off the end of the line
for(n=0; fname[n]; n++) if(fname[n] == '\n') fname[n]='\0';

System calls you'll need are unlink to remove files, open to read or create files, read to read from files, write to write to files, and close to close file descriptors once you're done.

Include files you'll need for all those are

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

System calls for cp and mv

Which system calls are made for operations cp and mv (2 Replies)
Discussion started by: gaurava99
2 Replies

2. UNIX for Dummies Questions & Answers

System calls?

open, creat, read, write, lseek and close Are they all primitive? :confused: *Another Question: is there a different between a system call, and an i/o system call? (2 Replies)
Discussion started by: PlunderBunny
2 Replies

3. Solaris

System calls ?

where can i find the differences in System calls between solaris and aix? also is it possible to find a comprehensive list of them? (1 Reply)
Discussion started by: TECHRAMESH
1 Replies

4. UNIX Desktop Questions & Answers

Using system calls

Hi, I'm new to UNIX system calls. Can someone share your knowledge as to how exactly system calls should be executed? Can they be typed like commands such as mkdir on the terminal itself? Also, are there any websites which will show me an example of the output to expect when a system call like... (1 Reply)
Discussion started by: ilavenil
1 Replies

5. Programming

System calls

why user is not able to switch from user to kernel mode by writing the function whose code is identical to system call. (1 Reply)
Discussion started by: joshighanshyam
1 Replies

6. BSD

system calls

what is the functions and relationship between fork,exec,wait system calls as i am a beginer just want the fundamentals. (1 Reply)
Discussion started by: sangramdas
1 Replies

7. UNIX for Dummies Questions & Answers

About system calls.

Hi all, I am new here . I want to know about system call in detail. As system calls are also function .How system identifies it.:) (2 Replies)
Discussion started by: vishwasrao
2 Replies

8. UNIX for Dummies Questions & Answers

system calls in C

Hello, how would i be able to call ps in C programming? thanks, ---------- Post updated at 01:39 AM ---------- Previous update was at 01:31 AM ---------- here's the complete system call, ps -o pid -p %d, getpit() (2 Replies)
Discussion started by: l flipboi l
2 Replies

9. Programming

Are system calls in c language only????

Hi friends, I have three questions. 1) What are system calls? 2) Is it necessary that system calls be in c language (in unix operating system)? 3) Importance of c language when programming in unix environment??? Looking forward to your wonderful replies! ... (2 Replies)
Discussion started by: gabam
2 Replies

10. Programming

System calls and C language low-level qualities???

Hi friends, I hope everyone is fine and doing well. I queried in my previous thread about the low-level qualities of C/C++ languages.I really thank you people for explaining, it was really helpful. One more ambiquity that I have in my mind is regarding the unix system calls like open, creat,... (1 Reply)
Discussion started by: gabam
1 Replies
PCAP_OPEN_OFFLINE(3)					     Library Functions Manual					      PCAP_OPEN_OFFLINE(3)

NAME
pcap_open_offline, pcap_fopen_offline - open a saved capture file for reading SYNOPSIS
#include <pcap/pcap.h> char errbuf[PCAP_ERRBUF_SIZE]; pcap_t *pcap_open_offline(const char *fname, char *errbuf); pcap_t *pcap_fopen_offline(FILE *fp, char *errbuf); DESCRIPTION
pcap_open_offline() is called to open a ``savefile'' for reading. fname specifies the name of the file to open. The file can have the pcap file format as described in pcap-savefile(5), which is the file format used by, among other programs, tcpdump(8) and tcpslice(1), or can have the pcap-ng file format, although not all pcap-ng files can be read. The name "-" in a synonym for stdin. Alternatively, you may call pcap_fopen_offline() to read dumped data from an existing open stream fp. Note that on Windows, that stream should be opened in binary mode. RETURN VALUE
pcap_open_offline() and pcap_fopen_offline() return a pcap_t * on success and NULL on failure. If NULL is returned, errbuf is filled in with an appropriate error message. errbuf is assumed to be able to hold at least PCAP_ERRBUF_SIZE chars. SEE ALSO
pcap(3), pcap-savefile(5) 5 April 2008 PCAP_OPEN_OFFLINE(3)
All times are GMT -4. The time now is 08:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy