0

I'm trying to make 2 different programs - one that will write notes to a note file, and one that will read the notes. These two programs are called Notes and Noteread. Here is the source code for these programs.

// notes.cpp
include <iostream>                                                              
#include <fstream>                                                               
#include <ctime>                                                                 
using namespace std;                                                             

int main () {                                                                    
    time_t now = time(0);                                                        
    char* dt = ctime(&now);                                                      

    ofstream myfile;                                                             
    std::cout << ": ";                                                           
    string x{};                                                                  
    std::cin >> x;                                                               

    myfile.open ("~/notes.txt", ios_base::app);                                  
    myfile << "---- " << dt;                                                     
    myfile << x << '\n';                                                         
    myfile << "\n";                                                              
    myfile.close();                                                              
    return 0;                                                                    
}                                                                                
// noteread.cpp
#include <bits/stdc++.h>                                                         

int main() {                                                                     
    system("less ~/notes.txt");                                                  
}                                                                                

It's supposed to work like this.

jingle@variable [time] [~/notes] [master *]
-> % notes
: hello
jingle@variable [time] [~/notes] [master *]
-> % noteread
( less program)
--- time
hello
jingle@variable [time] [~/notes] [master *]
-> %

Although it ends up doing this:

jingle@variable [time] [~/notes] [master *]
-> % notes
: hello
jingle@variable [time] [~/notes] [master *]
-> % noteread
/home/jingle/notes.txt: No such file or directory
jingle@variable [time] [~/notes] [master *]
-> %

Another thing i did to debug this is make the notes.txt file and then run the program, but that turns out once running the program like i did above noteread finds the file but the file's blank. This is really weird and beyond my knowledge. Thanks in advance!

2 Answers2

0

~/notes.txt isn't a "real" path. When you write it in your shell/terminal, it's expanded into /home/jingle/notes.txt. You wrote no code into your C++ program to do that.

I suggest you use a relative path instead, so just notes.txt. Document this fact, and invoke your program accordingly from the command-line with the proper current working directory for the task.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • It DOES work this way, and in fact i used it before and it worked, but i want this to be universal. Meaning: 1 - I can run it from everywhere. (i can put it in /bin) 2 - It doesn't make a file where you are. I'd rather have that be an option. – electricalveins May 18 '20 at 22:16
  • @electricalveins Okay you know best then – Asteroids With Wings May 19 '20 at 13:19
0

I used std::getenv to fix my problem. Then, i could get the HOME variable and use it in my program.