File Pointers in Seperate Functions

This is a discussion on File Pointers in Seperate Functions within the C Programming forums, part of the General Programming Boards category; I opened a file in my main() function like this FILE *myfile = fopen("myfile.txt","r"); I want to use the file ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    5

    File Pointers in Seperate Functions

    I opened a file in my main() function like this

    FILE *myfile = fopen("myfile.txt","r");

    I want to use the file for input in another function called writeData.

    How do I pass the file pointer *myfile to writeData?

    Im using GCC and get a Segmentation Fault if I pass 'myfile'.
    What parameters should the writeData function contain? (I used 'const char *file')

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    58
    const char *file is wrong

    myfile is of type FILE*, so that's your type.

    Edit: plus you opened your file as read only, so if you pass it writedata (assuming it DOES write data) you'll get an error

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Oh I didnt notice that!
    I changed it to 'w'.

    Anyway, Im really new to C so can you tell me exactly what I need to use?

    Thanx in advance

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    ie,
    Code:
    size_t writeData(FILE * outputStream, const char * data)
    {
        return /* your write function goes here, write 'data' to 'outputStream': eg http://www.cplusplus.com/reference/clibrary/cstdio/fwrite.html */
    }
    
    int main(void)
    {
        FILE * myFile;
        if((myFile = fopen("myfile.txt", "wt")) == NULL)
        {
            perror("Failed to open the file");
            return 1;
        }
    
        /* write to the file */
        writeData(myFile, "hello world");
    
        fclose(myFile);
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    5
    Code:
    int input(char * dest, FILE * inputStream, const char tochar)
    /*
    INPUT gets characters from a stream until the character specified in 'tochar' is reached OR until the entire
    return string 'dest' is filled (does not have space for more characters)
    Returns the number of characters read from 'inputStream'
    INPUT could be used to read characters until the end of the line by passing '\n' as tochar
    */
    
    {
    char c;
    int i = 0;
    int len = strlen(dest);
    int d = 0;
    
    
    	for (d = 0; d != len; d++)		// Fill up dest with NULL characters to remove any left over garbage
    	{
    		dest[d] = NULL;		
    	}
    
    
    		for (i = 0; c != tochar; i++)
    		{
    			c = getc(inputStream);
    			if (c == NULL) break;		// Check for END OF FILE
    			if (i != len)			// Check to see if the array has space to add another character
    				{
    				if (c != tochar) dest[i] = c;  // Add character to position [i] in dest if it != tochar
    				}
    				else
    				{
    				return i;  // In this case, INPUT doesnt return everything until 'tochar' b/c dest is
    				}	   // not long enough
    		}
    	return i;	// Return number of characters processed into dest
    }
    When I use this function, if my 'dest' is dimensioned to 20 (char a[20]) in my main program, everything works fine. However when I try to dimension it to 100 and pass it to INPUT, it doesnt read any characters at all into 'dest', and INPUT returns 0. Im using GCC. Is this a limitation of using console apps in linux or something? Or am I coding wrong, because as far as I can see, I cant find any bugs in this function. I tried using malloc() to allocate a space of memory for 'dest' in main() and then pass it into this function, but it didnt work.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    if (c == NULL) break;		// Check for END OF FILE
    you want to check for EOF, not NULL

    http://www.cplusplus.com/reference/c...tdio/getc.html

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    From the description of what input is supposed to do I assume that dest could be an unintialized buffer .
    Calling len = strlen(dest) would asign a random number to len. You will have to pass the size of the buffer to the input function.
    Code:
    		for (i = 0; c != tochar; i++) {
    			c = getc(inputStream);
    c is not intialized -> the loop may or may not run
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 12:54 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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