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