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 04-09-2012, 06:50 PM   #1
connect2janu
LQ Newbie
 
Registered: Jun 2011
Posts: 20

Rep: Reputation: Disabled
syntax errors


q1: C programme that accepts valid file names as command line arguments and for each of the arguments, prints the type of the file (regular file, directory file, character special file, block special file,
symbolic link, etc)
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
 
int main(int argc,char *argv[])
{
int i;
struct stat buf;
 
for(i=1;i<argc;i++)
{
printf("%s",argv[i]);
if(lstat(argv[i],&buf)==-1)
{
printf(" Lstat error\n");
continue;
}
if(S_ISREG(buf.st_mode))
printf(" Regular file\n");
if(S_ISDIR(buf.st_mode))
printf(" Directory file\n");
 
if(S_ISCHR(buf.st_mode))
printf(" Character file\n");
if(S_ISBLK(buf.st_mode))
printf(" Block file\n");
if(S_ISLNK(buf.st_mode))
printf(" Symbolic link file\n");
}
}
/////////////////////////////////////////////////////
q2: C programme to do the following: Using fork() create a child process. The child process prints its own process id and id of its parent and then exits. The parent waits for the childto finish(by executing wait()) and prints its own process id and the id of its child and then exits.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<sys/types.h>

 
int main()
{
pid_t ppid,mpid,pid,status=0;
pid=fork();
if(pid<0)
{
printf("Error");
exit(0);
}
 
if(pid==0)
{
mpid=getpid();
printf("\n I am child, my id is %d",mpid);
ppid=getppid();
printf("\n I am parent, my id is %d ",ppid);
exit(1);
}
 
pid=waitpid(pid,&status,0);
mpid=getpid();
printf("\n I am parent with id %d and my child is %d \n",mpid,pid);
}
both giving sysntax error,where is the mistake,can any rectify it,i have tried[in fedora 12-linux] i couldnt got solution
 
Old 04-09-2012, 07:11 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Well, a few thoughts:

1. This looks like homework. You can have people do your homework for you today, but some other time, you might not have that luck. My advice would be to do it on your own, or ask your instructor for help.

2. Both programs compile and work OK on my system.

3. You should include the error messages you see, when you post on a form, and also discuss what you've tried to do to fix them.
 
  


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
coldbackup file errors syntax issue need help wdsmith45 Linux - Newbie 2 02-21-2012 06:24 PM
bash Arithmetic Syntax Errors MTK358 Programming 23 08-15-2011 11:04 AM
couple C++ syntax errors bru Programming 8 05-16-2004 05:35 PM
Gimp-1.2.3 'make' reports syntax errors GabeF Linux - Software 2 06-11-2003 08:29 PM
declaration syntax errors with KYLIX rklosinski Programming 1 11-28-2002 01:49 PM

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

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