Function to open file

This is a discussion on Function to open file within the C Programming forums, part of the General Programming Boards category; Hey i am trying to make a function to open the file but am having trouble. The original code looks ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    Function to open file

    Hey i am trying to make a function to open the file but am having trouble. The original code looks lie:
    Code:
    fp=fopen(argv[1], "r"); 
    
             if(text==NULL) {
    
                EMPTY_FILE(1);
    
             }
             else {
    
                while((fgets(text[i], SIZE, fp)!=NULL)) {
    
                   i++;
    
    
                }
    And this was my attempt to turn it into a function including the prototype and the function after the main:
    Code:
    int READ_FILE(char filename[], char text[][SIZE+1]);//prototype
    
    READ_FILE(argv[1], text);//this part is in the main code
    
    //And this part is after the main
    int READ_FILE(char filename[], char text[][SIZE+1]){
       int i=0;
       FILE * fp;
       
           fp=fopen(filename, "r"); 
    
             if(text==NULL) {
    
                EMPTY_FILE(1);
    
             }
             else {
    
                while((fgets(text[i], SIZE, fp)!=NULL)) {
    
                   i++;
    
    
                }
             return i;
    }
    }
    I know this code doesn't work because it doesn't compile and i don't know why
    I am fairly new to making functions and implementing them.

    Any help is greatly appreciated
    Last edited by slowprogrammer; 05-27-2010 at 06:10 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by slowprogrammer View Post
    I know this code doesn't work because it doesn't compile and i don't know why
    What compiler are you using? If your compiler does not issue errors and an explanation for why it will not compile (most of them do), you should switch to a different one, because otherwise programming is just going to be one frustration after another.
    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

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    well the compiler says:
    Code:
    In function 'main'
    error: passing argument 2 of 'READ_FILE' from incompatible pointer type
    In function "READ_FILE":
    error: control reached end of non-void function
    I am not sure what the first error means but for the second one its usually a bracket problem and even though i have fixed that it still comes up (meaning its not fixed)...

    I was hoping you could tell me if I have the structure of the function right and if i am passing the relevant variables into it...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's your unclosed brace:

    Code:
    int READ_FILE(char filename[], char text[][SIZE+1]){
       int i=0;
       FILE * fp;
       
           fp=fopen(filename, "r"); 
    
             if(text==NULL) {
    
                EMPTY_FILE(1);
    
             }
             else {
    
                while((fgets(text[i], SIZE, fp)!=NULL)) {
    
                   i++;
    
    
                }
             return i;
    }
    As for the "incompatible pointer type" you'll have to post the declaration of "text".
    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

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    here's how i declared text:
    Code:
    char text[LENGTH][SIZE];
    //This is done in the main function...
    Text is basically and array where i copy the file i open into...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by slowprogrammer View Post
    Code:
    int READ_FILE(char filename[], char text[][SIZE+1]);//prototype
    Quote Originally Posted by slowprogrammer View Post
    here's how i declared text:
    Code:
    char text[LENGTH][SIZE];
    What oh what could possibly be the difference here?

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    cheers mate....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

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