Reputation: 11
I am building a small network of Raspberry Pi's using Raspian with samba file sharing. I want each to be able to read/write small text files to the others, to provide a very simple method of process co-ordination. I am a testing the method with one Pi with a single shared file and can read/write to it from an Ubuntu laptop, using its desktop file manager. I am trying to do the same programmatically:
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
void read_file () {
string line;
ifstream myfile;
myfile.open("\\\\192.168.1.78\\home\\pi\\new2.txt");
if (myfile.is_open()){
cout << "file is open" << endl;
getline(myfile, line);
cout<< line << endl;
} else {
perror("Error");
}
myfile.close();
}
int main(){
read_file();
}
The error message is 'No such file or directory'. I'm guessing this is either a permissions problem I've not seen (the remote file is shared as 777) or the way I'm referencing the server and folder. I've tried numerous combinations to no avail.
Any suggestions?
Upvotes: 1
Views: 1133
Reputation: 6004
Using backslash paths like this wouldn't work on an Ubuntu system. The fact that this works in your file manager is probably due to the file manager being smart and handling mounting and path conversion transparently for you.
What you need to do is to mount the shared folder yourself so you can use a regular Linux file path to open it. See this question on how to mount SMB shares with Samba: https://unix.stackexchange.com/questions/99065/how-to-mount-a-windows-samba-windows-share-under-linux
Upvotes: 1