AndroidDev
AndroidDev

Reputation: 21237

File pointers in an array

Very raw with C. I'm writing a program that takes files as it's arguments, but this is rather annoying for debugging (GDB). Rather than have to re-type the file list each time that I start off in GDB, I'd rather store the file names in an array and modify the program to read this array rather than the argv[] values.

I started out with

FILE*[5] inpFiles;
inpFiles[0] = &file1.txt;

but this is all wrong. I need to get some sort of reference to each of the input files so that I can get its memory address.

How can I do this? Thanks.

Upvotes: 1

Views: 1571

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753785

An array of FILE * would be written:

FILE *inpFiles[5];

This can store the values returned from fopen() or a similar functions; it does not store file names.

You might store the file pointer into the structure that &file1 represents, or you might create a new structure that stores the name and the opened file pointer (though you may need to specify a mode; presumably "r" or "rb" by default).

So, clarify to yourself what exactly you want to do. You can create an array of file pointers, or an array of structures containing, amongst other things, a file pointer. But you have to decide how you're going to use it and what the semantics are going to be.


This presumes that modifying the program is a better idea than using GDB better. If you can learn to use the facilities of GDB more powerfully, then that's a better idea.

For example, can you make it easy to specify the files by using a metacharacter:

run debug?.txt

where your files are debug0.txt, debug1.txt, ...?

The other answers also suggest alternatives.

Upvotes: 1

SeedmanJ
SeedmanJ

Reputation: 444

You can parse your input files, containing each of your files, by reading on the standard file stream 0.

so that you could do this:

 ./your_program < your_input_file

and in gdb,

  run/r < your_input_file

but if you want to keep your args method, you can also do this:

  ./your_program `cat your_input_file`

Hope this helps.

Upvotes: 2

timrau
timrau

Reputation: 23058

You can define a GDB command in .gdbinit so you don't need to modify your production code. For example, add the following lines in your ~/.gdbinit or .gdbinit in your working directory.

define do
    run file1.txt file2.txt file3.txt file4.txt file5.txt
end

Then, in GDB, just type the command do, and GDB runs run file1.txt file2.txt file3.txt file4.txt file5.txt for you.

Upvotes: 3

Related Questions