input text file - use debugger?

This is a discussion on input text file - use debugger? within the C Programming forums, part of the General Programming Boards category; Hi guys! Lets say I have the below program (K&R, 1.9), which simply does: while (there's another line) if (it's ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Unhappy input text file - use debugger?

    Hi guys!

    Lets say I have the below program (K&R, 1.9), which simply does:

    while (there's another line)
    if (it's longer than the previous longest)
    (save it)
    (save its length)
    print longest line

    Code:
    #include <stdio.h>
    #define MAXLINE 1000 /* maximum input line length */
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    /* print the longest input line */
    main()
    {
    	int len; /* current line length */
    	int max; /* maximum length seen so far */
    	char line[MAXLINE]; /* current input line */
    	char longest[MAXLINE]; /* longest line saved here */
    	max = 0;
    	while ((len = getline(line, MAXLINE)) > 0)
    		if (len > max) {
    			max = len;
    			copy(longest, line);
    		}
    	if (max > 0) /* there was a line */
    		printf("%s", longest);
    	return 0;
    }
    /* getline: read a line into s, return length */
    int getline(char s[],int lim)
    {
    	int c, i;
    	for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
    		s[i] = c;
    	if (c == '\n') {
    		s[i] = c;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }
    /* copy: copy 'from' into 'to'; assume to is big enough */
    void copy(char to[], char from[])
    {
    	int i;
    	i = 0;
    	while ((to[i] = from[i]) != '\0')
    		++i;
    }
    How I can give a text file, and use a debugger and see how it works? I want to understand somethings ... is that possible?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    If you really want to make the program take input from a file, you shouldn't have to make any coding changes, just run something like

    executable < test.txt

    In IDEs setting that up is dead simple since there should be a place to change command line arguments somewhere in the project settings.

    All you have to do is invoke your debugger when you are ready and step through the lines.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Quote Originally Posted by whiteflags View Post
    All you have to do is invoke your debugger when you are ready and step through the lines.
    How to invoke my debugger? I use xCode ... any suggestion?

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I want to understand somethings ... is that possible?
    That would depend solely on you, wouldn't it?

    I understand that Xcode comes with a debugger (I looked it up on Wikipedia), or rather a front-end for gdb. Try using that? There should be a "Debug" button or similar on the toolbar.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    I think this impossible! Since I need to debug (build) my program successfully before i give an input text etc. So how come my debugger will be involved when the program already has created successful object code... Lets hope some experience users to clarify this question... and disprove what msh and whiteflags had said!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by c_lady View Post
    I think this impossible! Since I need to debug (build) my program successfully before i give an input text etc. So how come my debugger will be involved when the program already has created successful object code... Lets hope some experience users to clarify this question... and disprove what msh and whiteflags had said!
    Well it's a little hard to disprove the truth...

    Debuggers typically make use of an additional symbol file (often with a .pdb extension or some such) to make sense of the already compiled file that you are executing when you debug things.

    If there is no built exe first, then what you have is an interpreter, not a compiler, and I'm not sure if any of those exist for the C programming language.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by c_lady View Post
    I think this impossible! Since I need to debug (build) my program successfully before i give an input text etc. So how come my debugger will be involved when the program already has created successful object code... Lets hope some experience users to clarify this question... and disprove what msh and whiteflags had said!
    First of all you have to compile it in "debug" mode with debugging symbols imbedded into the resulting obj and exe files... Then the debugger just follows what the program is doing...

    It's done all the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21