0

I have created a program to copy a certain base to different directories. Since I am doing this copying multiple times I made this program. However the program functions properly in the directory where the program exists. But it won't work in other directories, only an empty file is created.

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[]) {

    string s;
    cin>>s;
    fstream base;
    fstream dest;
    base.open("base_layout.cpp",ios::in);
    dest.open(s,ios::out);
    string a;
    while(getline(base,a)){
       dest<<a<<"\n";
       cout<<a;
    }
    base.close();
    dest.close();
    return 0;
}

The base file exists in the directory where the program exists. The input for the program is "filename.txt" (filename can be anything)

halfer
  • 19,824
  • 17
  • 99
  • 186
Athul Das
  • 11
  • 1
  • 2
  • Did you check what is read in `s`? – Damien Oct 26 '20 at 14:21
  • Maybe related: https://stackoverflow.com/questions/843083/open-file-by-its-full-path-in-c – Damien Oct 26 '20 at 14:23
  • Relative paths are relative to the current [*working directory*](https://en.wikipedia.org/wiki/Working_directory) of the process. For your program, if the current directory isn't the exact same as where the source file is, then the opening of the file stream will fail. – Some programmer dude Oct 26 '20 at 14:31
  • the s is contains the file name. – Athul Das Oct 27 '20 at 05:58
  • @Someprogrammerdude the path is correct because it is creating a new file in the directory I mention. However it isn't copying the information from the source file. I don't know what is the error – Athul Das Oct 27 '20 at 06:03
  • How do you run your program? From a terminal? Is the input file in the same directory as from where you run the program? If you run `ls` before running your program, is the input file listed? – Some programmer dude Oct 27 '20 at 06:41
  • @Someprogrammerdude thanks for your advice, the source file had the error due to relative directory. Putting an absolute directory solved the problem. – Athul Das Oct 27 '20 at 09:17

0 Answers0