|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I am new to C, literally. My lovely professor wants us to teach ourselves to program while he compiles it and tells us we're wrong without going into any details. If it compiles and it runs = A, if it doesn't = F. There's no inbetween. By the way, this is to be compiled using "gcc" in an UNIX environment
part a: First of all, you are supposed to write a program in either C or C++ that reads data from a file and copies it to another file. Your program should copy all contents of the file. part b: Secondly, write another program to create child processes from your main program recursively using fork( ) and exec( ) commands up to a certain level, for example, fifth level. Then print identification of each child process in each level. My code: Part A:Code: Code:
#include<stdio.h>
#include <string.h>
int main(void)
{
// open a text file for reading
FILE *in_file
in_file = fopen("input.txt","r");
// open a text file for writing
FILE *out_file
out_file = fopen("output.txt","w");
if(!f)
{ return 1; }
while(fgets(s,80,in_file) != NULL)
{
//write to file
fprintf(out_file, "%s",s");
fprintf(out_file, "\n");
}
fclose(in_file);
fclose(out_file);
system("pause");
return 0;
}
Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
/* call the recursive function */
recurse(5);
}
void recurse(int count)
{
Pid_t pid;
pid = fork();
if(pid < 0)
{
/* if the fork failed give error */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
/* child process and print the ID*/
execlp("/bin/ls", "ls", NULL);
printf("Child Process ID: " + pid);
}
else
{
/* parent process */
/* parent will wait for the child to complete */
wait (NULL);
exit(0);
}
/* if the count is less than one, we're finished */
if(count > 1)
{
exit(0);
}
/* decrement count and call itself (recursion) */
else
{
count = count - 1;
recurse(count - 1)
}
}
Last edited by WingedPanther; 03-24-2008 at 10:51 AM. Reason: add code tags |
| Sponsored Links |
|
|
|
|||||
|
Are you getting error messages when you try to compile? If so what?
Is it compiling but not running properly?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Beginners calendar exercise | Jude | Java Help | 2 | 09-07-2008 09:55 AM |
| Beginners Guide To PHP: *Tutorial* | renlok | PHP Tutorials | 3 | 04-22-2008 03:20 PM |
| Forums for photoshop design and designers .. also for beginners? | navjeet | Computer Software/OS | 0 | 08-21-2007 12:29 AM |
| Good book's for beginners? | Vojkan | HTML Programming | 4 | 02-28-2007 07:02 PM |
| A site for beginners in the Technology Field | littlefranciscan | Technology Ramble | 2 | 01-16-2007 07:52 AM |
| Xav | ........ | 1357.94 |
| MeTh0Dz|Reb0rn | ........ | 1083.85 |
| WingedPanther | ........ | 919.18 |
| marwex89 | ........ | 906.86 |
| morefood2001 | ........ | 903.18 |
| John | ........ | 902.37 |
| Brandon W | ........ | 789.89 |
| chili5 | ........ | 312.39 |
| Steve.L | ........ | 264.99 |
| dcs | ........ | 240.34 |
Goal: 100,000 Posts
Complete: 83%