C - file not opening

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,443
Country
Canada
I'm going to do a complete rewrite my sad excuse of an emulator that doesn't even work and I'm having some trouble with opening the file. It should first open zenity (which works), then copy the output of zenity, which is the path to whatever file they chose, then parse the output and put backslashes in front of spaces (which works), but whenever I actually try to open the file (a file that does exist) it always fails, I looked around a bit more and found this so I commented out the
Code:
path[n++] = '\\';
line but to no avail. Can anyone offer some advice as to what I'm doing wrong?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * select_file(void)
{
    FILE *fp = popen("zenity --file-selection --title=\"Choose Gameboy Rom\"", "r");

    static char path[1024];
    memset(path, 0, sizeof(path));

    if(fp == NULL)
    {
        printf("Failed to run command, make sure you have zenity installed!\n" );
        exit(1);
    }
    fgets(path, sizeof(path), fp);
    pclose(fp);

    char path_copy[256];
    strcpy(path_copy, path);

    int n = 0;
    for(int i = 0; i < sizeof(path); i++)
    {
        if(path_copy[i] == ' ')
        {
            path[n++] = '\\';
        }

        path[n++] = path_copy[i];
    }

    return path;
}

int main(void)
{
    FILE * fp = NULL;
    char file_to_open[1024];
    strcpy(file_to_open, select_file());
    printf("%s", file_to_open);
    fp = fopen(file_to_open, "r");
    if (fp == NULL)
    {
        perror("Error opening file: ");
    }

    return 0;
}
 

TheZoc

Well-Known Member
Member
Joined
Apr 6, 2009
Messages
317
Trophies
1
Age
39
Location
Rio de Janeiro
XP
1,850
Country
Brazil
From the looks of it, your code reminds me of unix/linux code, yet you're using windows paths.

Try using '/' in place of '\\' and see if it works ;)
 

Monado_III

Well-Known Member
OP
Member
Joined
Feb 8, 2015
Messages
722
Trophies
0
Location
/dev/null
XP
1,443
Country
Canada
From the looks of it, your code reminds me of unix/linux code, yet you're using windows paths.

Try using '/' in place of '\\' and see if it works ;)
the '\'s are for spaces (think escape sequences), the actually path that you get from printf is something like /home/*user*/somedir/some\ file.txt, I'm not sure if the backslashes are needed in C but it doesn't seem to make a difference as nothing opens either way
 
Last edited by Monado_III,

spoonm

Can count to 3.
Member
Joined
May 20, 2015
Messages
192
Trophies
0
Website
spoonm.org
XP
317
Country
Brazil
the '\'s are for spaces (think escape sequences), the actually path that you get from printf is something like /home/*user*/somedir/some\ file.txt, I'm not sure if the backslashes are needed in C but it doesn't seem to make a difference as nothing opens either way

The escape slashes are not needed. I just created a file called "test file" with the contents:

Code:
Can I open this file? Can I?
Will it fail, my while?

I like coconut water.

, then wrote, compiled and ran the following:

Code:
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    FILE *fp = NULL;
    int c;

    if ((fp = fopen("test file", "r")) == NULL)
    {
        perror("Error opening file");
        exit(1);
    }

    while ((c = fgetc(fp)) != EOF)
        putchar(c);

    fclose(fp);

    return EXIT_SUCCESS;
}

And it's worked fine:

ndqdaj.png
 
Last edited by spoonm,
  • Like
Reactions: Monado_III

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    NinStar @ NinStar: because I already got burned out with breath of the wild, which for the most part is essentially...