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,866
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
  • No one is chatting at the moment.
  • K3Nv2 @ K3Nv2:
    @Veho, where's the poll at?
  • Julie_Pilgrim @ Julie_Pilgrim:
    they're giving the internet an open text box?
  • BigOnYa @ BigOnYa:
    I never played or own any of they devices, but I like small style, but with a somewhat grip
  • Julie_Pilgrim @ Julie_Pilgrim:
    oh man, that always goes great!
  • Julie_Pilgrim @ Julie_Pilgrim:
    im sure half the responses won't be literal racial slurs or "drop table" jokes
  • Veho @ Veho:
    Look, it's China. They know what it's like when you give a poll to half a billion trolls.
  • K3Nv2 @ K3Nv2:
    How much dollar do you think it is?
  • Veho @ Veho:
    ONE MILLION DOLLA
  • Veho @ Veho:
    I know the pricing of electronics nowadays isn't "how much it actually costs" but "how much we can get away with", but putting up a poll is just cynical.
  • K3Nv2 @ K3Nv2:
    Probably $150 someone said Anbernic said around the same price as rg556
  • Julie_Pilgrim @ Julie_Pilgrim:
    you know which game i wish they would rerelease
  • Julie_Pilgrim @ Julie_Pilgrim:
    sonic unleashed
  • K3Nv2 @ K3Nv2:
    Make it a happy meal toy
  • Julie_Pilgrim @ Julie_Pilgrim:
    that game's engine is really fucking intensive so it runs like literal shit on xbox 360 and ps3
  • Veho @ Veho:
    Nah I'm getting value creep again. I look at a $50 console "but for just a few more dollars you could get XYZ" and I end up considering the Steam Deck.
  • Julie_Pilgrim @ Julie_Pilgrim:
    like the lighting in that game was genuinely so good
  • Veho @ Veho:
    Not getting dragged into that again.
  • Julie_Pilgrim @ Julie_Pilgrim:
    i dont get why they didn't port the one game that ran the worst on consoles, to pc
  • Julie_Pilgrim @ Julie_Pilgrim:
    like you port everything to pc except the one game where it would make the most sense. why. what do you gain from this
  • Julie_Pilgrim @ Julie_Pilgrim:
    is sega just personally fucking with me? are they laughing while watching me through my kinect camera as i get up to restart my xbox for the third time because the game froze again
  • K3Nv2 @ K3Nv2:
    Buy handhelds from five below better quality
  • K3Nv2 @ K3Nv2:
    Valve probably going to do another refresh of the deck this fall with rog ally like specs tbh
  • Veho @ Veho:
    A smaller form factor would be nice too.
  • K3Nv2 @ K3Nv2:
    A shield portable 2 would be nice aye Nvidia
    +1
    K3Nv2 @ K3Nv2: A shield portable 2 would be nice aye Nvidia +1