Author Topic: Problem opening file  (Read 1477 times)

Offline ordak

  • Multiple posting newcomer
  • *
  • Posts: 100
    • My Site
Problem opening file
« on: August 11, 2017, 11:54:01 am »

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: [Select]
// Template Hello World 4

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

void simple_err(const char *const 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;
Code::Blocks SVN 11925
OS : Ubuntu LTS

Offline Miguel Gimenez

  • Lives here!
  • ****
  • Posts: 569
Re: Problem opening file
« Reply #1 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.