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,848
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
  • BigOnYa @ BigOnYa:
    Ok xdqwerty, your little bro prob tripped On the cord and unplugged you
  • K3Nv2 @ K3Nv2:
    Ya I'm afraid of the dark hug me
  • BigOnYa @ BigOnYa:
    Grab and hold close your AncientBoi doll.
  • K3Nv2 @ K3Nv2:
    Damn didn't charge my external battery either
  • BigOnYa @ BigOnYa:
    Take the batteries out of your SuperStabber3000... Or is it gas powered?
  • K3Nv2 @ K3Nv2:
    I stole batteries from your black mamba
    +1
  • K3Nv2 @ K3Nv2:
    My frozen food better hold up for an hour I know that
  • BigOnYa @ BigOnYa:
    Or else gonna be a big lunch and dinner tomorrow.
  • BigOnYa @ BigOnYa:
    Did you pay your power bill? Or give all yo money to my wife, again.
  • K3Nv2 @ K3Nv2:
    Oh good the estimated time is the same exact time they just said
    +1
  • BigOnYa @ BigOnYa:
    Load up your pc and monitor, and head to a McDonalds dining room, they have free WiFi
  • K3Nv2 @ K3Nv2:
    Sir please watch your porn in the bathroom
    +2
  • BigOnYa @ BigOnYa:
    No sir we can not sell you anymore apple pies, after what you did with the last one.
  • K3Nv2 @ K3Nv2:
    We ran out
  • HiradeGirl @ HiradeGirl:
    for your life
    +1
  • K3Nv2 @ K3Nv2:
    My life has no value my fat ass is staying right here
  • K3Nv2 @ K3Nv2:
    Nearly 4 hours without power :(
  • Veho @ Veho:
    SO POWERLESS
  • K3Nv2 @ K3Nv2:
    Tell Kanye I need power
  • DinohScene @ DinohScene:
    Better start running in your hamster wheel
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    Meth addicts on a treadmill connected to a generator "Unlimited POWER!!!'
  • Veho @ Veho:
    Before or after a hit?
    Veho @ Veho: Before or after a hit?