Author Topic: How to debug when the inputs getted from a text file?  (Read 775 times)

Offline wanggaoteng

  • Multiple posting newcomer
  • *
  • Posts: 28
How to debug when the inputs getted from a text file?
« on: May 27, 2019, 09:07:18 am »
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:
Code: [Select]
#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.
« Last Edit: May 27, 2019, 09:27:16 am by wanggaoteng »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2493
Re: How to debug when the inputs getted from a text file?
« Reply #1 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...

Offline wanggaoteng

  • Multiple posting newcomer
  • *
  • Posts: 28
Re: How to debug when the inputs getted from a text file?
« Reply #2 on: May 28, 2019, 04:15:50 am »
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.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2493
Re: How to debug when the inputs getted from a text file?
« Reply #3 on: May 28, 2019, 04:01:08 pm »
Your ptoject structure on your harddisk should be something like this:
Code: [Select]
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:
Code: [Select]
exampleProject
|-- exampleProject.cbp
|-- test.txt
|-- bin
      |-- Debug
             |--- exampleProject.exe

Offline wanggaoteng

  • Multiple posting newcomer
  • *
  • Posts: 28
Re: How to debug when the inputs getted from a text file?
« Reply #4 on: May 28, 2019, 06:36:53 pm »
Your ptoject structure on your harddisk should be something like this:
Code: [Select]
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:
Code: [Select]
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?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2493
Re: How to debug when the inputs getted from a text file?
« Reply #5 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
Code: [Select]
cat < test.txtpart is set in Project->Set program arguments

If i were you I would embedd the file reading in your code...
Code: [Select]
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;
}

Offline wanggaoteng

  • Multiple posting newcomer
  • *
  • Posts: 28
Re: How to debug when the inputs getted from a text file?
« Reply #6 on: May 30, 2019, 09:34:55 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
Code: [Select]
cat < test.txtpart is set in Project->Set program arguments

If i were you I would embedd the file reading in your code...
Code: [Select]
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.