# Filepath in pi c

**URL:** <https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313>\
**Category:** Uncategorized\
**Created:** [25 November 2024 07:19 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313 "2024-11-25T07:19:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clem73188](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clem73188](https://forum.core-electronics.com.au/u/Clem73188)\
**Post date:** [25 November 2024 07:19 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/1 "2024-11-25T07:19:24Z")

</div>

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:

```auto
#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?

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [26 November 2024 00:19 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/2 "2024-11-26T00:19:02Z")

</div>

> [@Clem73188](#):
>
> `if(( fp = fopen( "/home/pi/TextAccess/testfile.txt", "r")) == NULL) { // this works`

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.

> [@Clem73188](#):
>
> if(( fp = fopen( “[//192.168.1.246/home/pi/TextAccess/testfile.txt](https://192.168.1.246/home/pi/TextAccess/testfile.txt)”, “r”)) == NULL) { // NOT work

This is the correct file path as far as I am aware.

> [@Clem73188](#):
>
> but first testing on the same machine.

This may also be causing you issues.

---

<div class="post-metadata">

**Author:** ![Clem73188](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clem73188](https://forum.core-electronics.com.au/u/Clem73188)\
**Post date:** [26 November 2024 05:17 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/3 "2024-11-26T05:17:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Clem73188](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clem73188](https://forum.core-electronics.com.au/u/Clem73188)\
**Post date:** [26 November 2024 05:29 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/4 "2024-11-26T05:29:56Z")

</div>

THis is a screen shot of the File Explorer from a windows machine:

 ![TestFile02](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/3/3a79e4172263c708d65fd4d612d398f6d1f98692.jpeg)

the C code expression:  
if(( fp = fopen( “[//192.168.1.243/\_AZtest/testfile.txt](https://192.168.1.243/_AZtest/testfile.txt)”, “r”)) == NULL) { // NOT work

---

<div class="post-metadata">

**Author:** ![Liam120347](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam120347/32/5003_2.png) [@Liam120347](https://forum.core-electronics.com.au/u/Liam120347)\
**Post date:** [26 November 2024 06:33 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/5 "2024-11-26T06:33:43Z")

</div>

Hi Clem,

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)

---

<div class="post-metadata">

**Author:** ![Clem73188](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clem73188](https://forum.core-electronics.com.au/u/Clem73188)\
**Post date:** [26 November 2024 09:15 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/6 "2024-11-26T09:15:36Z")

</div>

Yes: Here are the screen shots from the Pi.:

 ![PiFileTest04](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/7/78b57aaea5d5caa1aac6aad18bcf79351c565a2e.png)  
 ![PiFileTest01](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/8/845b9e5cea5155b2fc6fe0601d3970cbc0db3a12.jpeg)  
 ![PiFileTest02](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/8/897e03ecbc8f05d4138de8a6941e7cba7b2fd96f.png)  
 ![PiFileTest03](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/3/37e7e4f72bc36e3934ba8aa74c98e09332321da3.png)

---

<div class="post-metadata">

**Author:** ![Clem73188](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@Clem73188](https://forum.core-electronics.com.au/u/Clem73188)\
**Post date:** [26 November 2024 14:45 UTC](https://forum.core-electronics.com.au/t/filepath-in-pi-c/21313/7 "2024-11-26T14:45:31Z")

</div>

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
