Home > database >  Multiple failed attempts to open a text file in C
Multiple failed attempts to open a text file in C

Time:02-18

I have been spending the past hour trying to figure out how to display a text document programmatically in c .

The following is the simple main.cpp code, in which I create a directory "Profiles", and another directory "TestProfile" inside. In Profiles/TestProfile I create a "TestFile" and write "Test data" into it.

#include <iostream>
#include <fstream>
#include <sys/stat.h>

#include "Profile.hpp"
#include <stdlib.h>

using namespace std;
int main(int argc, const char * argv[]) {
    
    ofstream fs;
    
    mkdir("/Users/dario/Desktop/Profiles", S_IRWXU);
    mkdir("/Users/dario/Desktop/Profiles/TestProfile", S_IRWXU);
    fs.open("/Users/dario/Desktop/Profiles/TestProfile/TestFile");
      
    fs << "Test data\n";
    
   
    string path = "/Users/dario/Desktop/Profiles/TestProfile/TestFile";
    system(path.c_str());

    
    return 0;
}

Everything up until line 30 works as expected. The directories are there, and "TestFile" is present with data written. Directories

There are no errors present when compiling, but ./a.out gives me a permission denied

darios-mbp:FMES dario$ g   -std=c  2a main.cpp 
darios-mbp:FMES dario$ ./a.out 
sh: /Users/dario/Desktop/Profiles/TestProfile/TestFile: Permission denied

I tried a few approaches.

First, I checked the permissions of the text file using ls -l, it returned -rw-r--r--, so I used chmod 755. After that, the file permissions said -rwxr-xr-x. When running the same code, ./a.out returned no permission denied and it seems like it worked, but the text file was not opened.

Secondly, after the first approach didn't work, I deleted the directories and ./a.out to recreate the original permission denied issue.

I was getting permission denied as before, and this time I tried changing the ownership or the directory.

sh: /Users/dario/Desktop/Profiles/TestProfile/TestFile: Permission denied
darios-mbp:FMES dario$ chown -R $USER  
/Users/dario/Desktop/Profiles/TestProfile/TestFile
darios-mbp:

The command worked, but running the code still did nothing despite no errors.

I tried chmod 755, but again no file was opened.

On previous attempts to fix this issue, after changing the ownership the same way I just explained, I even had an error where it was trying to run the first word of the textfile as a command

Line 1: Test: command not found

Although I was not able to recreate that while writing this.

Perhaps I am doing something completely wrong, but I looked as much as I could for solutions online before writing this post. None of them seemed to work. Perhaps my situation is unique in some way and someone here could help point me in the right direction. I apologize in advance if my post is in any way not to standard (if so, please let me know how I can improve questions for next time).h

I am running the code on xcode

EDIT

Thanks to @Codo for the answer, the following edit did what I was looking for

string path = "/Users/dario/Desktop/Profiles/TestProfile/TestFile";
string cmd = "open -a TextEdit  ";
system(cmd.c_str());

CodePudding user response:

I understand that you want to open the created file with an appropriate application. Let's assume the file is /dir/file.

To open a file from the command line / terminal, you would run:

open /dir/file

Or, if you want a specific applications:

open -a TextEdit /dir/file

Also see The macOS open Command.

If you just run:

/dir/file

you would execute the file. That's probably not what you intended. And execution is only possible if the file meets certain criteria like execution rights and in the case of a shell script an appropriate header.

In C , the system() function executes a command that would be valid on the command line (in the terminal).

Thus, to fix your program, change the line with system() to:

string cmd = "open -a TextEdit ";
cmd  = path;
system(cmd.c_str());

And for your next question: Paste code as text, not as image: Why not upload images of code/errors when asking a question?

  • Related