Home › Forums › C Programming › Beginner’s help
- This topic has 1 reply, 2 voices, and was last updated 16 years, 9 months ago by Humayan.
- AuthorPosts
- March 24, 2008 at 8:10 am #2084Ms.RParticipant
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:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283#include<stdio.h><br />#include <string.h><br />int main(void)<br />{<br />// open a text file for reading<br />FILE *in_file<br />in_file = fopen("input.txt","r");<br /><br />// open a text file for writing<br />FILE *out_file<br />out_file = fopen("output.txt","w");<br /><br /><br />if(!f)<br />{ return 1; }<br /><br />while(fgets(s,80,in_file) != NULL)<br />{<br />//write to file<br />fprintf(out_file, "%s",s");<br />fprintf(out_file, "n");<br />}<br /><br />fclose(in_file);<br />fclose(out_file);<br />system("pause");<br />return 0;<br />}<br /><br />Part B:Code:<br /><br />#include <stdio.h><br />#include <unistd.h><br /><br />int main()<br />{<br />/* call the recursive function */<br />recurse(5);<br />}<br /><br /><br />void recurse(int count)<br />{<br />Pid_t pid;<br />pid = fork();<br /><br />if(pid < 0)<br />{<br />/* if the fork failed give error */<br />fprintf(stderr, "Fork Failed");<br />exit(-1);<br />}<br /><br />else if (pid == 0)<br />{<br />/* child process and print the ID*/<br />execlp("/bin/ls", "ls", NULL);<br />printf("Child Process ID: " + pid);<br />}<br /><br />else<br />{<br />/* parent process */<br />/* parent will wait for the child to complete */<br />wait (NULL);<br />exit(0);<br />}<br /><br /><br />/* if the count is less than one, we're finished */<br />if(count > 1)<br />{<br />exit(0);<br />}<br /><br />/* decrement count and call itself (recursion) */<br />else<br />{<br />count = count - 1;<br />recurse(count - 1)<br />}<br />}<br />Please helpe me?!!?! What am I doing wrong? What am I missing? This guy told me it’d be easy because I “know” Java and it isn’t. I honestly think learning Ada was a lot easier than C.
- March 24, 2008 at 4:39 pm #3370HumayanParticipant
For the first part you could do:
12345678910111213141516171819202122232425262728#include<br />#include <br />int main(void)<br />{<br />// open a text file for reading<br />FILE * in_file;<br />in_file = fopen("c:\programs\help\text1.txt","r");<br />// open a text file for writing<br />FILE * out_file ;<br />out_file = fopen("c:\programs\help\text2.txt","w");<br /><br />if ( ! in_file || ! out_file )<br />{<br />printf ( "a file did not openn" );<br />return 1;<br />}<br />char ch = fgetc ( in_file );<br />while ( ch != EOF )<br />{<br />//write to file<br />fputc ( ch , out_file );<br />ch = fgetc ( in_file );<br />}<br />fclose(in_file);<br />fclose(out_file);<br />// system("pause");<br />return 0;<br />}
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.