Thread: More trouble

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    More trouble

    Hi, I'm having another episode with function parameters, but I've included the source files.

    Thanks for your help.

    Code:
    /* 	Write	- 	This module writes an array of 60 converted characters into	*/
    /*				the output file generated by the software.					*/
    
    #include <stdio.h>
    
    void Write (FILE **PtrfilePtr, const char *Chargroup)	char Character;
    	
    	while (Index < 60)
    	{
    {
    	int Index = 0;
    
    		Character = Chargroup[Index];
    		putc(Character, *PtrfilePtr);
    	}
    	
    	putc("|", *PtrfilePtr);
    	
    }

    Code:
    /*	Convert.c -		This module is the main loop of the conversion engine.	*/
    /*					It fills an array of 60 characters amd converts and     */
    /*					writes the encrypted characters.						*/
    
    #include <stdio.h>
    
    
    /*	Convert() -	Read a group of 60 characters.							*/
    
    void Convert(FILE **InfilePtr, FILE **OutfilePtr)
    {
    	int Character, Index;
    	char Chargroup[59];
    
    	Index = 0;
    	Character = getc(*InfilePtr);
    	
    	while (Character != EOF)
    	{
    		Chargroup[Index] = Character;
    		
    		if (Index == 59)
    		{
    			Write(*OutfilePtr, Chargroup);
    			Index = 0;
    		}
    		
    		else 
    		{
    			Index ++;
    		}
    		
    		Character = getc(*InfilePtr);
    	}
    
    	printf("%s", Chargroup);
    		
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Was there a question you meant to ask? Maybe include some error messages?
    Last edited by pianorain; 01-21-2005 at 10:34 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Of course. I'm sorry.

    I've pinpointed the problem to the write functions. The compiler says there's a parse error on lines with Chargroup or filepntr.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    Write(*OutfilePtr, Chargroup);
    The write function expects a FILE** and a const char*. You are passing it a FILE* and a char*.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    I see. Should I pass is as so:

    write (**OutfilePtr, Chargroup);

    And take away the const declaration in the prototype?

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'm surprised the compiler even gets that far.
    Code:
    void Write (FILE **PtrfilePtr, const char *Chargroup)  // char Character; // this doesn't go here.
    {
       char Character; //it does go here
       int Index = 0; //might work better outside of the while loop
       while (Index < 60)
       {
    //{ //we can get rid of this
    //    int Index = 0;  //this needs to be moved
          Character = Chargroup[Index];
          putc(Character, *PtrfilePtr);
    
          //you might want to change Index somewhere inside this while loop
          //otherwise, it'll be an infinite loop
       }	
    // putc("|", *PtrfilePtr); //putc puts a character, not a string
       putc('|',*PtrFilePtr);
    }
    And you need to pass it as:
    Code:
    write (OutfilePtr, Chargroup);
    OutfilePtr is already a FILE**; you don't want to dereference it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    And then there is the write function itself. I'm not sure if you messed up pasting your code or not, but there are some obvious problems with it. Did you mean to do this?

    Code:
    #include <stdio.h>
    
    void Write (FILE **PtrfilePtr, const char *Chargroup)
    {
    	char Character;
    	int Index = 0;
    
    	while (Index < 60)
    	{
    		Character = Chargroup[Index];
    		putc(Character, *PtrfilePtr);
    		Index++;
    	}
    	
    	putc('|', *PtrfilePtr);
    	
    }

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I see. Should I pass is as so:

    write (**OutfilePtr, Chargroup);

    And take away the const declaration in the prototype?
    No, you should pass it as:
    Code:
    Write(OutfilePtf, Chargroup);

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Thanks, there were some obvious problems there - most shamefull, thanks for pointing them out.

    But I don't understand why I would pass the file pointer as a non-pointer. Why?

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You're not passing it as a non-pointer. OutfilePtf is a FILE**, and that is what you are passing.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    I understand. But what about the function prototype?

  12. #12
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    There is a difference between the use of the * in this:
    Code:
    char* pCharacterPointer;
    and this:
    Code:
    int anotherInt = *pIntPointer
    The first one is declaring a character pointer. In the second one, the * is being used as the indirection operator. The indirection operator allows you to get at the value being pointed at by the pointer. Consider the following code sample:
    Code:
    int main()
    {
       int a = 3;  //a = 3
       int *b = &a;  //b now points to a
    
       *b = 7;  //the value that b points to = 7
       printf("%d",a);  //should output 7
       return 0;
    }
    In your code, OutfilePtf is a FILE**. The expression *OutfilePtf is a FILE*, and the expression **OutfilePtf is a FILE. By adding more *'s, you're actually getting further away from the solution.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Okay, I understand now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM