-1

I am trying to copy the contents of one file to another in linux. I think my logic is correct but I don't understand what the error is.

My function take 3 parameters. The 3rd parameter is a string which is the file name from which content is supposed to be read.

#include<iostream>
#include <curses.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
void process(int cvar, int cclause, string fnm)
{
    ifstream fs;
    ofstream ft;

    fs.open("contents.txt");
    if(!fs)
    {
        cout<<"Error in opening source file..!!";
    }
    ft.open(fnm,ios::app);
    if(!ft)
    {
        cout<<"Error in opening target file..!!";
        fs.close();
    }

char str[255];
while(fs.getline(str,255))
{
    ft<<str;
}



    cout<<"File copied successfully..!!";
    fs.close();
    ft.close();
    getch();
}

And this is the error I am getting:

g++ mainProj.cpp -lz3
/tmp/ccLBpiRs.o: In function `process(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
mainProj.cpp:(.text+0x172): undefined reference to `stdscr'
mainProj.cpp:(.text+0x17a): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
  • BTW, you say that you think your logic is correct, but you also say that the third parameter is the filename from which to read (supposedly), while your code is clearly appending to it. – stefaanv Jun 24 '19 at 13:17
  • BTW, a faster, more efficient method for copying files is to open them in binary mode and use `istream::read` and `ostream::write` with `uint8_t` arrays. The best technique for copying files is to let the Operating System copy the files for you. – Thomas Matthews Jun 24 '19 at 14:10
  • Do you have an example program that utilises istream::read and ostream::write? – GoldParticle Jun 24 '19 at 17:46

3 Answers3

1

#include <ncurses.h> and link with -lncurses.

More here.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
0

How to transfer contents from one file to another in Ubuntu.?

You can use input stream to read from a file, and output stream to write into a file.

mainProj.cpp:(.text+0x172): undefined reference to `stdscr'
mainProj.cpp:(.text+0x17a): undefined reference to `wgetch'

You've included a header <curses.h> and used function declared there, but you've failed to link against a library that defines those functions.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

How to transfer contents from one file to another in Ubuntu.?

Here's a simple efficient snippet. There are more efficient methods:

#include <iostream>  
#include <fstream>
#include <string>  

void copy_file(const std::string&  source_filename, const std::string& destination_filename)
{
    std::ifstream input(source_filename.c_str(), "b");
    std::ofstream output(destination_filename.c_str(), "b");
    const size_t BUFFER_SIZE = 1024 * 16;
    static uint8_t buffer[BUFFER_SIZE];
    while (input.read(buffer, BUFFER_SIZE))
    {
        const size_t bytes_read = input.gcount();
        output.write(buffer, bytes_read);
    }
}

The above code uses a large buffer. File contents are read (using binary mode), into the buffer, then written to another file, using block I/O. Files are streaming devices and are most efficient when transferring {large} blocks of data.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154