User forums > General (but related to Code::Blocks)

Problem opening file

(1/1)

ordak:
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;
}
--- End code ---

Miguel Gimenez:
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.

Navigation

[0] Message Index

Go to full version