skip to Main Content

I have a problem I cant solve for the past 2 weeks. In my school we have a course where we are learning about opoerating systems and we are forced to work within WSL environment, in Ubuntu.

One of the tasks is to create a simple program that reads from and writes to files and then is used to receive signals (SIGINT, SIGTERM etc.), but the trouble I am having is that the program crashes immediately when I run it.

It worked well when I simply compiled and ran it inside Windows environment but now when I want to use file locations inside Ubuntu home directory (so that I can access those via Ubuntu terminal) it doesnt work, it crashes immediately upon starting.

int main()
{
    //main part of program

    int broj;
    FILE *fptr1;
    FILE *fptr2;

    fptr1=fopen("\wsl.localhost\Ubuntu\home\asevic\lab1\obrada.txt", "r+");
    fptr2=fopen("\wsl.localhost\Ubuntu\home\asevic\lab1\status.txt", "r+");

    int n =0;

    if(fptr1 == NULL)
    {
        printf("Error!");
        exit(1);
    }

    if(fptr2 == NULL)
    {
        printf("Error!");
        exit(2);
    }
    while(fscanf( fptr2, "%d", &n ) == 1);

    fclose(fptr2);
    printf("Program s PID=%ld krenuo s radomn", (long) getpid());
    printf( "The last number in the file is: %dn", n );

    fptr2=fopen("\wsl.localhost\Ubuntu\home\asevic\lab1\status.txt", "r+");

    int kv, counter=n+1;
    int i;
    for(i=n+1;i<1000;++i)
    {
        kv=i*i;
        fprintf(fptr1,"%dn",kv);
        fprintf(fptr2,"%dn",counter);
        printf("Next square value: %dn", kv);
        counter++;
        sleep(3);
    }
    printf("Program endsn");
    
    return 0;
}

this is the code I am using, I believe the issue is in the file paths. But those are the paths to the Ubuntu home directory where the c file, and the to be created txt files are located.

Can anyone shed some light on the issue?

Regards,
Adrian

Like I said, the program works within the Windows environment but fails when Ubuntu home directory file paths are included. I also get an "Error!" message when trying to run from Ubuntu terminal, signalling tghat files are not being created.

2

Answers


  1. Those are the Windows file paths, but you’re running the program in WSL. Your paths need to be relative to the / route. Also, UNIX paths use forward slashes, not backslashes.

    The correct route would be ~/lab1/obrada.txt where the tilde is the /home/USER directory

    Login or Signup to reply.
    1. Network paths (UNC paths) start with \ (two backslashes). So you need to escape them twice:
    "\\wsl.localhost\Ubuntu\home\asevic\lab1\status.txt"
    
    1. If you run program under WSL or native Linux use slashes / and different paths
    "/home/asevic/lab1/status.txt"
    
    1. Check Windows WSL path using wslpath -w /home/asevic/lab1/status.txt command.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search