source-code coversion

please i have this copy file program which i need too be converted into see to be used in Unix C. it works in Unix C with the g++ compiling but i need it in C because i have other functions that are in C and need to call the functions in my made shell. this is the code below;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
       string src_file, dest_file;

       cout << "Enter the file name to copy: ";
       getline(cin, src_file);

       // Make sure the file exists.
       ifstream fin;
       fin.open(src_file.c_str());
       if (fin.fail())
       {
               cout << "File '" << src_file << "' does not exist.\n";
               return -1;
       }

       cout << "Enter the destination file name: ";
       getline(cin, dest_file);

       // See if the dest file already exists
       ifstream temp;
       temp.open(dest_file.c_str());
       if (!temp.fail())
       {
               temp.close();
               cout << "The file '" << dest_file << "' already exists.\n"
                       << "Do you want to over-write this file (y/n)? ";
               char choice;
               cin >> choice;
               if (choice != 'y' && choice != 'Y')
                       return -1;
       }

       // Copy each char from fin into fout
       ofstream fout;
       fout.open(dest_file.c_str());
       char c = fin.get();
       while (!fin.eof())
       {
               fout << c;
               c = fin.get();
       }

       fin.close();
       fout.close();

       cout << "\nDone.\n";
       return 0;
}


would be really grateful if replied to. thank you all
If I understand you correctly, & you just want the C version of this code then I think this should help - compiles fine with "gcc file.c -o file":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
  // character arrays to hold the strings
  // FILENAME_MAX: http://www.cplusplus.com/reference/clibrary/cstdio/FILENAME_MAX/
  char src_file[FILENAME_MAX];
  char dest_file[FILENAME_MAX];

  // write string to stdout
  printf("Enter the file name to copy: ");
  // request user input, into src_file variable
  fgets(src_file, FILENAME_MAX, stdin);
  // remove the new line character from the end of the string
  src_file[ strlen(src_file) - 1 ] = '\0';

  // create a FILE variable
  // declare it to open the previously inputted file
  // "rb" - read as binary
  FILE* fin = fopen(src_file, "rb");

  // if an error occured reading the file
  if( fin == NULL )
  {
    // print the message "File error" with the error generated
    perror("File error");
    // exit program
    return -1;
  }

  // once again write to stdout & request user input
  printf("Enter the destination file name: ");
  fgets(dest_file, FILENAME_MAX, stdin);
  dest_file[ strlen(dest_file) - 1 ] = '\0';

  // check if the file exists
  if( !access(dest_file, F_OK) )
  {
    // prompt the user to over-write if it does
    printf("The file '%s' already exists.\nDo you want to over-write this file  (y/n)? ", dest_file);
    // get input choice
    char choice;
    choice=getchar();
    if( choice != 'y' && choice != 'Y' )
    {
      // if they do not want to over-write, return
      return -1;
    }
  }

  // open the file for writing
  // "wb" - write as binary
  FILE* fout = fopen(dest_file, "wb");
  char c;
  // loop through all the input characters
  while( (c = fgetc(fin)) != EOF )
  {
    // write them to the output file
    fputc( (int)c, fout );
  }

  // close input & output files
  fclose( fin );
  fclose( fout );

  puts("\nDone.");

  return 0;
}
Last edited on
Topic archived. No new replies allowed.