User forums > Using Code::Blocks
Using textfiles with Code Blocks
Nudelholz:
Hello !
I use Code Blocks (MinGW) and wrote a small program that analyses the chars of a textfile called testscript.txt and puts them in a char array. This is a code snippet how I read out the textfile char by char:
short c = fgetc(stdin);
After compilation, I start the program in the Windows-cmd with "program.exe < testscript.exe". It works but I can't debug (step by step) in Code::Blocks when executing the program this way.
Is it possible to add the testscript.txt file to the Code::Blocks project, so the textfile is appended the same way (< testscript.txt) everytime I press the "Run"-button ?
Thx for help and sorry for my bad english ! :D
btw: I'm using SVN 4545
thomas:
--- Quote ---program.exe < testscript.exe
--- End quote ---
You probably meant to type program.exe < testscript.txt ?
You can provide command line options under the menu "Project" --> "Set program's arguments". This is as close to what you want as possible. It does not redirect input, though (which you want). You could modify your program to work that way by adding 3-4 lines of code, though (which is the "correct" behaviour, anyway).
You can get input reditrection if you add a post-build step like cmd /C program.exe < testscript.txt. Note that this will run the program every time you build, and it will not show a console, but dump the output to the log window. Also, if your program freezes, Code::Blocks will wait forever (or until you kill the program).
--- Quote ---short c = fgetc(stdin);
--- End quote ---
As a sidenote, this is the most inefficient way to read a file (you probably know). Reading in big chunks (or the whole file in one go) is often a lot faster.
Nudelholz:
--- Quote ---program.exe < testscript.exe
You probably meant to type program.exe < testscript.txt ?
--- End quote ---
Yes :)
--- Quote ---You can provide command line options under the menu "Project" --> "Set program's arguments". This is as close to what you want as possible. It does not redirect input, though (which you want). You could modify your program to work that way by adding 3-4 lines of code, though (which is the "correct" behaviour, anyway).
--- End quote ---
Yes, I want to redirect input. Another way would be to use FILE, fopen etc. but I couldn't make it work.
--- Quote ---You can get input reditrection if you add a post-build step like cmd /C program.exe < testscript.txt. Note that this will run the program every time you build, and it will not show a console, but dump the output to the log window. Also, if your program freezes, Code::Blocks will wait forever (or until you kill the program).
--- End quote ---
It works but it doesn't help me debugging step by step.
--- Quote ---short c = fgetc(stdin); As a sidenote, this is the most inefficient way to read a file (you probably know). Reading in big chunks (or the whole file in one go) is often a lot faster.
--- End quote ---
I know but I have to do it this way (our chief-developer said). Just to let you know, the topic is RFID which sometimes seems to require a very special way of coding :)
I'm also new to this sector.
Thank you so far !
thomas:
I would try something like this:
--- Code: ---#include <stdio.h>
/* beginning of main() */
FILE *f;
f = argc > 1 ? fopen(argv[1], "rb") : stdin;
short *c = fgetc(f); /* or whatever */
fclose(f);
/* end of main() */
--- End code ---
Admittedly, it is no proper implementation of parsing commandline options, but it would allow you to read from either stdin or a file given as commandline option, and the latter would allow you to debug, too :)
Overhead is practically zero, and fgetc (or fread) should work just the same.
Nudelholz:
This doesn't work for me, too, because the function to read every single Byte is not in main() but in another .c-file.
The code looks something like this:
--- Code: ---short recordByte() {
for(;;) {
short c = fgetc(stdin);
if(isxdigit(c)) {
unsigned char retval;
if(isdigit(c)) {
retval = ...
}
.....
.....
c = fgetc(stdin);
if(!isxdigit(c)) {
....
}
return retval;
} else if(....) {
.....
.....
.....
}
return ...
}
}
--- End code ---
Errors, EOL, EOF, \n etc. are handled inside this routine. There is another function in this .c-file that calls recordByte, saves the return value and writes it to a buffer. The main() function is far away :)
It seems ridiculous but I can't get it work using FILE, fopen etc....
Navigation
[0] Message Index
[#] Next page
Go to full version