Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: ordak on August 11, 2017, 11:54:01 am

Title: Problem opening file
Post by: ordak on August 11, 2017, 11:54:01 am
Hi,

When I run the following code from Code::Blocks it runs as expected, however if I run it from Terminal in Ubuntu it fails. What can I do?

The code taken from Internet with some modification:

Code
// Template Hello World 4

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

void simple_err(const char *const str)
{
    fprintf(stderr,"%s\n",str);
    exit (EXIT_FAILURE);
}

int main (void)
{
    FILE *fp;
    fp = fopen ("input_file.text","r");
    if (fp == NULL)
        simple_err("Problem opening file.");

    fclose (fp);
    printf ("File opened okay\n");

    return EXIT_SUCCESS;
}
Title: Re: Problem opening file
Post by: Miguel Gimenez on August 11, 2017, 02:12:39 pm
The default directory is usually different in both environments, within CB the directory is "." (where the CBP file resides) and in a terminal it is where you set it (usually the executable directory). So, depending where the txt file is, the executabe finds it or not.

To correct this in Project -> Properties -> Build targets the output filename dir and the execution working dir should be the same.