Author Topic: Using textfiles with Code Blocks  (Read 13584 times)

Nudelholz

  • Guest
Using textfiles with Code Blocks
« on: October 22, 2007, 04:50:26 pm »
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
« Last Edit: October 22, 2007, 05:00:34 pm by Nudelholz »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Using textfiles with Code Blocks
« Reply #1 on: October 23, 2007, 09:03:21 am »
Quote
program.exe < testscript.exe
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);
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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Nudelholz

  • Guest
Re: Using textfiles with Code Blocks
« Reply #2 on: October 23, 2007, 11:05:25 am »
Quote
program.exe < testscript.exe
You probably meant to type program.exe < testscript.txt ?

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).

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).

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.

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 !

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Using textfiles with Code Blocks
« Reply #3 on: October 23, 2007, 05:34:40 pm »
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() */
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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Nudelholz

  • Guest
Re: Using textfiles with Code Blocks
« Reply #4 on: October 24, 2007, 09:37:30 am »
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 ...
        }
}

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....
« Last Edit: October 24, 2007, 10:02:04 am by Nudelholz »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Using textfiles with Code Blocks
« Reply #5 on: October 24, 2007, 12:31:09 pm »
Quote
This doesn't work for me, too, because the function to read every single Byte is not in main() but in another .c-file.
Now be serious :) Then turn recordByte() into recordByte(FILE* f) (and the other function likewise), and pass on the file descriptor. Or, if you think that is too much hassle, just use a global variable which you initialise in main(). Global variables are not pretty, but they do the job for such a purpose.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."