Thread: defining functions

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    16

    defining functions

    Hello...

    I am trying to modify a program from a text book and I have a puzzeling question.

    The text tries to output information from a file to the printer using the stdprn, however, on Linux, this is undefined, so I decided to define it.

    However, I am finding that I have to define it in 2 locations in the program for it to work proporly. Is there a way I can define it in one location and not throughout the code?

    Here is the code and it does compile and work. I am defining prn for referance:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void do_heading(char *filename);
    
    int line = 0, page = 0;
    
    int main( int argv, char *argc[] )
    
    {
      char buffer[256];
      FILE *prn;                                    /* First definition */
      prn = popen("/usr/bin/lpr", "w");  /* First definition */
      FILE *fp;
      if( argv < 2 )
      {
            fprintf(stderr, "\nProper Usage is: ");
            fprintf(stderr, "\n\nprint_it filename.ext\n" );
            return(1);
      }
    
      if (( fp = fopen( argc[1], "r" )) == NULL )
            {
                    fprintf(stderr, "Error opening file, %s!\n\n",argc[1]);
                    return(1);
            }
      page = 0;
      line = 1;
      do_heading( argc[1]);
      while( fgets( buffer, 256, fp ) != NULL )
            {
                    if (line % 55 == 0)
                            do_heading( argc[1] );
                            fprintf( prn, "%4d:\t%s", line++, buffer );
            }
      fprintf( prn, "\f" );
      fclose(fp);
      return 0;
            }
      void do_heading( char *filename )
      {
     FILE *prn;                                    /* Second Definition */
     prn = popen("/usr/bin/lpr", "w");  /* Second Definition */
            page++;
            if (page > 1)
                    fprintf( prn, "\f" );
            fprintf( prn, "Page: %d, %s\n\n", page, filename);
      }
    As I stated, this does compile and work as it is, but just seems silly to me to have it defined in 2 locations. I tried to put it first above the void and then in the main {}, but its not working that way.

    Any ideas on how to get this to work?

    Thanks!!!

    Joe

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Have your do_heading function take another parameter, FILE *prn, and then just pass a pointer to the file stream you opened in main.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by Memloop View Post
    Have your do_heading function take another parameter, FILE *prn, and then just pass a pointer to the file stream you opened in main.
    Well, if I put FILE *prn; in the do_heading function and take out the prn = popen (bla); but leave it in main, it seg falts when I run it.

    So maybe this is correct syntax, but it just seems a little silly to have to define these things in each function...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Show the code then.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Not sure what you want to see. I posted the code in the original message, then tried your method.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Post the new code where you get a segfault.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by Memloop View Post
    Post the new code where you get a segfault.
    Oh.. ok, here is the code that seg faults... I defined *prn once in main and set prn to the pipe of the printer, then in do_heading, I just specify *prn but not the pipe. This compiles, but seg faults.

    The only way I got it to work was defining *prn and setting prn = to the printer in both main and do_heading. If this is correct, I will live with it, it just seemed a little inefficient that way :-D.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void do_heading(char *filename);
    
    int line = 0, page = 0;
    
    int main( int argv, char *argc[] )
    
    {
      char buffer[256];
      FILE *prn;
      prn = popen("/usr/bin/lp", "w");
      FILE *fp;
      if( argv < 2 )
      {
            fprintf(stderr, "\nProper Usage is: ");
            fprintf(stderr, "\n\nprint_it filename.ext\n" );
            return(1);
      }
    
      if (( fp = fopen( argc[1], "r" )) == NULL )
            {
                    fprintf(stderr, "Error opening file, %s!\n\n",argc[1]);
                    return(1);
            }
      page = 0;
      line = 1;
      do_heading( argc[1]);
      while( fgets( buffer, 256, fp ) != NULL )
            {
                    if (line % 55 == 0)
                            do_heading( argc[1] );
                            fprintf( prn, "%4d:\t%s", line++, buffer );
            }
      fprintf( prn, "\f" );
      fclose(fp);
      return 0;
            }
      void do_heading( char *filename )
      {
       FILE *prn;
           page++;
            if (page > 1)
                    fprintf( prn, "\f" );
            fprintf( prn, "Page: %d, %s\n\n", page, filename);
      }

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You haven't done what I told you to do ... You should try to eliminate the global variables line and page as well if possible.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void do_heading(FILE *prn, char *filename);
    
    int line = 0, page = 0;
    
    int main( int argv, char *argc[] )
    
    {
    	char buffer[256];
    	FILE *fp;
    	FILE *prn;
    	prn = popen("/usr/bin/lp", "w");
    	if( argv < 2 )
    	{
    		fprintf(stderr, "\nProper Usage is: ");
    		fprintf(stderr, "\n\nprint_it filename.ext\n" );
    		return(1);
    	}
    
    	if (( fp = fopen( argc[1], "r" )) == NULL )
    	{
    		fprintf(stderr, "Error opening file, %s!\n\n",argc[1]);
    		return(1);
    	}
    	page = 0;
    	line = 1;
    	do_heading(prn, argc[1]);
    	while( fgets( buffer, 256, fp ) != NULL )
    	{
    		if (line % 55 == 0)
    			do_heading(prn, argc[1] );
    		fprintf( prn, "%4d:\t%s", line++, buffer );
    	}
    	fprintf( prn, "\f" );
    	fclose(fp);
    	return 0;
    }
    void do_heading(FILE *prn, char *filename )
    {
    	page++;
    	if (page > 1)
    		fprintf( prn, "\f" );
    	fprintf( prn, "Page: %d, %s\n\n", page, filename);
    }

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Joeg1484 View Post
    Oh.. ok, here is the code that seg faults... I defined *prn once in main and set prn to the pipe of the printer, then in do_heading, I just specify *prn but not the pipe. This compiles, but seg faults.
    Seg faults where/why? If you do not know how to answer this question, it is DEFINITELY time you learned to use the debugger (nb, otherwise you are asking someone to take a guess, or else run the code thru a debugger for you.)

    1) Compile with the -g switch, eg. "gcc -g mycode.c"
    2) Now run the program in gdb: "gdb ./a.out" where a.out is the name of the executable.

    At the gdb prompt, type "run". The program will run. When it segfaults, you will get an informative message telling you the source code line number and instruction which caused the problem. This is the simplest, and often handiest, use of gdb.

    You may also want to check this out:
    http://cboard.cprogramming.com/progr...linux-ddd.html

    Knowing where the segfault occurs may not totally solve your problem but it provides a very very helpful place to start.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by Memloop View Post
    You haven't done what I told you to do ... You should try to eliminate the global variables line and page as well if possible.
    OOOhhh.. Ok, I see what you are saying... Ok, I did that and it worked. Sorry, still learning here.

    That is much better... I have a lot to learn

    It is working perfectly now!!! Thanks for your help

    Joe

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    16
    Quote Originally Posted by MK27 View Post
    Seg faults where/why? If you do not know how to answer this question, it is DEFINITELY time you learned to use the debugger (nb, otherwise you are asking someone to take a guess, or else run the code thru a debugger for you.)

    1) Compile with the -g switch, eg. "gcc -g mycode.c"
    2) Now run the program in gdb: "gdb ./a.out" where a.out is the name of the executable.

    At the gdb prompt, type "run". The program will run. When it segfaults, you will get an informative message telling you the source code line number and instruction which caused the problem. This is the simplest, and often handiest, use of gdb.

    You may also want to check this out:
    http://cboard.cprogramming.com/progr...linux-ddd.html

    Knowing where the segfault occurs may not totally solve your problem but it provides a very very helpful place to start.
    Thanks for the pointers. I will try this method next time I get an error like that. I am still learning (3rd day with this).

    Thank!!!

    Joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Defining functions in C and using function arguments
    By Bluesilver2009 in forum C Programming
    Replies: 14
    Last Post: 10-28-2009, 01:29 PM
  3. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Defining Class memeber functions
    By silk.odyssey in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 05:50 PM