News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

using debugger to open file for program to read data in, is not seeing that file

Started by userx-bw, October 25, 2019, 04:56:51 PM

Previous topic - Next topic

userx-bw

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?

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

Pecan

Have you set the args for the program run by the debugger?

MainMenu/Project/Set program arguments...


userx-bw

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.

oBFusCATed

(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]