Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: userx-bw on October 25, 2019, 04:56:51 pm

Title: using debugger to open file for program to read data in, is not seeing that file
Post by: userx-bw on October 25, 2019, 04:56:51 pm
simple little program that opens a file then reads its contents, when using the debugger asknig for a filename it does not see that file in the same dir it is in, ../Debug/
hard code it in and it still does not see the file.

open a seperate terminal then goto where program is located along with the file, run it and it sees and opens then reads the file.

how to get the debugger to do the same?

Code
// a program to skip to the next line in file if the comment char (#) is encountered

#define  CONTENT_LEN 373
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void) {
/*
    if ( argc < 2)
    {
        printf("no file given\n");
        exit(1);

    }
    */

    char content[CONTENT_LEN];
    char filename[10] = "./test";
    int i = 0;
//    char *filename = NULL;
    size_t bufsize = 32;

    //  char * filename = strdup(argv[1]);

    FILE *testfile;
    /*
    filename = (char *)malloc(bufsize * sizeof(char));
       printf("Enter file name for test file\n");
       getline(&filename, &bufsize, stdin);
      */
       printf("%s\n", filename);

       if( access( filename, F_OK ) != -1 ) {

            printf("file exist\n");
        } else {
            printf("file doesn't exist\n");

        }

    if ( ( testfile = fopen(filename, "r")) == NULL ) {
         fprintf(stderr, "Input file cannot be read, aborting.\nDoes it exist?\n");
         return 1;
    }

    while ( fgets(content, CONTENT_LEN, testfile) != NULL ) {

          for ( i = 0; content[i] != '\0'; i++ ) {
  if (content[i] == '#')
{
int f = i;
if ((content[--f] == ' ') && (content[++f] == ' ')) {
break;
} else  {
printf("%s\n", content);
printf("%c", content[i]);
}
}
  /*
if (( content[i] == '#' ) && ( content[--i] != '\0' || content[--i] != ' ' || content[--i] != '\n'))
printf("line[%s]\n", content);
*/

}
                // break; // (4)  for loop stops after not equal to a space and displays the first char of the line, but because of the nested if, also looks for the hash
}

 //   printf("%i < %i (%c) ? %s (loop exited)\n", i, content[i], content[i], i<content[i] ? "True" : "False");

    fclose(testfile);

    return 0;
}
Title: Re: using debugger to open file for program to read data in, is not seeing that file
Post by: Pecan on October 25, 2019, 07:48:30 pm
Have you set the args for the program run by the debugger?

MainMenu/Project/Set program arguments...

Title: Re: using debugger to open file for program to read data in, is not seeing that file
Post by: userx-bw on October 25, 2019, 08:24:32 pm
OK in the "program arguments' text box.
I added absolute path and file name, now I got a figure out how to show values and show execution line steps, so I can follow it around in the loop.
 
Title: Re: using debugger to open file for program to read data in, is not seeing that file
Post by: oBFusCATed on October 25, 2019, 10:05:46 pm
Debug -> Windows -> Watches for examining values of variables.