News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Problem opening file

Started by ordak, August 11, 2017, 11:54:01 AM

Previous topic - Next topic

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:

// 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;
}

Code::Blocks SVN
OS : Ubuntu LTS

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.