News:

As usual while waiting for the next release - don't forget to check the nightly builds in the forum.

Main Menu

How to debug when the inputs getted from a text file?

Started by wanggaoteng, May 27, 2019, 09:07:18 AM

Previous topic - Next topic

wanggaoteng

Hi, when I solving the questions in my textbook, a problems appears. I want to debug using codeblocks, but the inputs which the program needs are quoted from a text file. For example, the nameof my project is "test", and the main program is:

#include "stdio.h"
int main(void)
{
    int ch;
    while((ch=getchar())!=EOF)
    {
        putchar(ch);
    }
    printf("\n");
    return 0;
}

Before running this program, I create a text file which named as "test.txt" in the same folder with .exe file(test.exe). Then I open the terminal, and typing "test.exe <test.txt", and the results appears.
I want to debug this program using codeblocks, is there any way to make the inputs getted from the test.txt file when debuging?

Best Regards.

BlueHazzard

You have to put the text file in the folder where the project file is.
If you run your application in codeblocks the base path is not the path of the executable, but of the project file...

wanggaoteng

Quote from: BlueHazzard on May 27, 2019, 12:59:58 PM
You have to put the text file in the folder where the project file is.
If you run your application in codeblocks the base path is not the path of the executable, but of the project file...

Hi, BlueHazzard,
I can't understand the messages above, it's abstract. Can you give me a detailed instruction?
Thank you.

BlueHazzard

Your ptoject structure on your harddisk should be something like this:

exampleProject
|-- exampleProject.cbp
|-- bin
      |-- Debug
             |--- exampleProject.exe
             |--- test.txt


but if you debug you have to put your test.txt in the root folder of the project, so that it looks like this:

exampleProject
|-- exampleProject.cbp
|-- test.txt
|-- bin
      |-- Debug
             |--- exampleProject.exe

wanggaoteng

Quote from: BlueHazzard on May 28, 2019, 04:01:08 PM
Your ptoject structure on your harddisk should be something like this:

exampleProject
|-- exampleProject.cbp
|-- bin
      |-- Debug
             |--- exampleProject.exe
             |--- test.txt


but if you debug you have to put your test.txt in the root folder of the project, so that it looks like this:

exampleProject
|-- exampleProject.cbp
|-- test.txt
|-- bin
      |-- Debug
             |--- exampleProject.exe

Hi, BlueHazzard,
Thanks for presenting the detailed message. I followed your instruction, but when the little yellow arrow reaches "while((ch=getchar())!=EOF)" in debug, then the arrow disappears, and I must input some message in the console. It is not my propose, I want to let the inputs (while((ch=getchar())!=EOF)) got from the text file (test.txt) when debugging. Is there anyway to do that?

BlueHazzard

It could be possible if you are on linux. On windows it is probably not that easy...
One possibility would be that you run your application from outside codeblocks and attach the debugger to it with Debug->Attach to process

You can try to go this way: https://stackoverflow.com/a/4758218
the
cat < test.txt
part is set in Project->Set program arguments

If i were you I would embedd the file reading in your code...

nt main()
{
   FILE *fp;

   fp = fopen("test.txt", "r"); // read mode

   if (fp == NULL)
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

    while((ch=fgetc(fp))!=EOF)
    {
        putchar(ch);
    }
   fclose(fp);
   return 0;
}

wanggaoteng

Quote from: BlueHazzard on May 29, 2019, 09:36:30 AM
It could be possible if you are on linux. On windows it is probably not that easy...
One possibility would be that you run your application from outside codeblocks and attach the debugger to it with Debug->Attach to process

You can try to go this way: https://stackoverflow.com/a/4758218
the
cat < test.txt
part is set in Project->Set program arguments

If i were you I would embedd the file reading in your code...

nt main()
{
   FILE *fp;

   fp = fopen("test.txt", "r"); // read mode

   if (fp == NULL)
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

    while((ch=fgetc(fp))!=EOF)
    {
        putchar(ch);
    }
   fclose(fp);
   return 0;
}


Hi, BlueHazzard,
It's my fault for not telling you that my computer is on ubuntu.
Following your instruction, I create a "text" file in the folder of .cbp, and typing "./test <text" in Project->Set program arguments, then I click the debug button, it solves my problem profectly.
Thank you.
Best Regards.