Home Forums C Programming Beginner’s help

Viewing 1 reply thread
  • Author
    Posts
    • #2084
      Ms.R
      Participant

      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
      #include
      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:

      #include
      #include

      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.

    • #3370
      Humayan
      Participant

      For the first part you could do:
       

      #include
      #include
      int main(void)
      {
       // open a text file for reading
       FILE * in_file;
       in_file = fopen("c:\programs\help\text1.txt","r");
       // open a text file for writing
       FILE * out_file ;
       out_file = fopen("c:\programs\help\text2.txt","w");

       if ( ! in_file || ! out_file )
       {
        printf ( "a file did not openn" );
        return 1;
       }
       char ch = fgetc ( in_file );
       while ( ch != EOF )
       {
        //write to file
        fputc ( ch , out_file );
        ch = fgetc ( in_file );
       }
       fclose(in_file);
       fclose(out_file);
      // system("pause");
       return 0;
      }
Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.