Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-2008, 08:09 AM
blacksaibot blacksaibot is offline
Newbie
 
Join Date: Mar 2008
Posts: 2
Rep Power: 0
blacksaibot is on a distinguished road
Default Beginner's help

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; 
}
Part B:Code:
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)
}
}
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.

Last edited by WingedPanther; 03-24-2008 at 10:51 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-24-2008, 10:53 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,035
Last Blog:
NaNoWriMo Day 23 The...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default Re: Beginner's help

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-24-2008, 05:03 PM
nutario's Avatar   
nutario nutario is offline
Newbie
 
Join Date: Mar 2008
Location: Germany
Posts: 23
Rep Power: 2
nutario is on a distinguished road
Send a message via ICQ to nutario Send a message via Skype™ to nutario
Default Re: Beginner's help

In Part A:
There are some ";" missing. (e.g FILE * in_file; )

I don't see the sence of this code part:
if(!f)
{ return 1; }
For me it is like that: You didn't declare "f".


For part B:
I only can repeat what WingedPanther said.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners calendar exercise Jude Java Help 3 07-30-2008 05:36 PM
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


All times are GMT -5. The time now is 11:41 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 66%

Ads