1

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.

3
  • Could you not create a batch file to start your gdb session with the files listed there? Commented Feb 20, 2013 at 0:37
  • 2
    Maybe what you need is a local .gdbinit. Commented Feb 20, 2013 at 0:37
  • Do you want to store the array of files, or the array of file NAMES? If the latter, you want something along the lines of the strangely deleted post by @laishiekai. (I've done this before, in similar circumstances, and it's a perfectly legit "trick" when debugging.) Commented Feb 20, 2013 at 1:01

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is what I'll do, since I don't have to modify my program this way.
2

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.

Comments

1

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.

1 Comment

Thank you. fopen() is mostly what I was missing to get a reference to the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.