I need help with these 2 programs

This is a discussion on I need help with these 2 programs within the C++ Programming forums, part of the General Programming Boards category; I am new to C, literally. My lovely professor wants us to teach ourselves to program while he compiles it ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    I need help with these 2 programs

    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:
    #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("input.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:
    #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.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    For Part A, you are writing to the same file you are reading from. Not impossible, but generally not a good idea.

    You've also never declared variable "s".

    fgets() will return "up to" 80 bytes, unless it finds a newline character first. If it doesn't find a newline in 80 bytes, you won't get one. By your indiscriminate writing of the newline, you are guaranteed to be changing the file as you write it. See here: http://www.cppreference.com/stdio/fgets.html

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Woops, I meant to change the code to output.txt that's why.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Should it be something like this?


    Code:
    void main(void)
    {
    	FILE *fp; 
    	int i;
    
    
    	if ((fp = fopen("myfile", "w"))==NULL)
    	{
    		printf("Cannot open file \n");
    		exit(1);
    	}
    
    	i=100;
    
    	if (fwrite(&i, 2, 1, fp) !=1)
    	{
    		printf("Write error occurred");
    		exit(1);
    	}
    
    	fclose(fp);
    
    
    	if ((fp =fopen("myfile", "r"))==NULL)
    	{
    		printf("Read error occurred");
    		exit(1);
    	}
    
     	printf("i is &#37;d",i);
     	fclose(fp);
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Should it be something like this?
    not really

    but for exact copy of the files - opening them in binary mode and using fread/fwrite pair could be more appropriate
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 06:32 AM
  2. I never Finish my programs :(
    By epidemic in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2007, 12:35 PM
  3. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 04:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21