I have a Pi and am writing a C programme to open a file on another machine. I have the same text file in diferent paths to test working. I can open the testfile.txt on the pi using file manager.
I can open the file on the Pi using the C code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
FILE *fp;
// if(( fp = fopen( "testfile.txt", "r")) == NULL) { // this works
// if(( fp = fopen( "/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // this works
// if(( fp = fopen( "//192.168.1.246/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
// if(( fp = fopen( "~/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
// if(( fp = fopen( "192.168.1.246/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
if(( fp = fopen( "192.168.1.246//home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
printf("could not open file\n");
exit(0);
}
fclose( fp);
return(0);
}
So: if(( fp = fopen( "testfile.txt", "r")) == NULL) { // this works
Similarly: if(( fp = fopen( "/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // this works
But this does not work, 192.168.1.246 is the IP address of the pi this is running on if(( fp = fopen( "//192.168.1.246/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
This does not work either: if(( fp = fopen( "192.168.1.246/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
Nor does this work: if(( fp = fopen( "192.168.1.246//home/pi/TextAccess/testfile.txt", "r")) == NULL) { // NOT work
I want to replace the IP address for another network connected machine, but first testing on the same machine. I have searched and this seems to be the format for full machine file path.
Can someone advise what is the correct full machine file path ? How can I get this to work?
This will work as you’re opening a file on your Pi. There are a range of factors that could be preventing this from working. Network restrictions will limit what you are able to do.
This is the correct file path as far as I am aware.
My current state is: My Pi is connected to a network with many other computers and other Pi’s on it. From any windows computer on the network I can open file explorer and look at the file system and open files including creating files and directories. Also from any Pi with the GUI I can open file Manager and see the file structure and open any file and create any directory and file. So it seems to me the network is corectly configures to do this. I do not want to have another extra peice of software running on the file server. I ned to be able to have one C file running on the Pi which can open a file on the file server and read from it and write to it.
I do not see why the fopen with "//192… etc does not work or what could be causing this to fail?
Are you able to run those same sanity checks with the file explorer on the Pi?
Windows makes the mounting process very easy, have you confirmed that the network drive is mounted on the Pi?
Also to consider in future is how your network drive handles multiple machines trying to access the same file (if that’s part of your project)
Well gues what!
I took careful note of the text in the FileManager which shows the link to the file when I put the mouse over the external file that I am trying to access and noted the following unusual text:
/run/user/1000/gvfs/smb-share:server=nas.local,share=data/_AZtest/testfile.txt
So I copied this in exactly to the function as:
if(( fp = fopen( “/run/user/1000/gvfs/smb-share:server=nas.local,share=data/_AZtest/testfile.txt”, “r”)) == NULL) {
AND - I works!!
I am yet to try writing to a file or creating a new file or creating a new directory or modifying / appending to a file,
But I am excited enough to have this open working that I had to post this.
I am also yet to see if this remains the same after rebooting the Pi or server - we will see.
Regards
2 Likes
And you can get our latest projects and tips straight away by following us on: