#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20

    Exclamation I'm stucked here.Please Help me.


    Dear friend,
    I am creating one header file which will help me to open the file and get it's content. As student of undergraduate i found C is not having sound functions and facility of string handling as compared to C++. So i consider it as bit difficult for me. And for this reason i lost one programming competition also despite completing the code firstly and precisely.
    Here is code of header file:
    I'm stuck in "OpeningFile" function.I am not getting way how to return data(string) in funtion which is read from file. Kindly give me your suggestion and if possible implement it in one example. I have tried it with "char* OpenFile(char*)" returning the data at the end but didn't get the results. So here is one another way tried by me. The error is in pointer(I think so) kindly guide me.
    Code:
    #ifndef FHANDLING_H_INCLUDED
    #define FHANDLING_H_INCLUDED
    
    
    #include<string.h>
    
    #include<stdio.h>
    
    
    int CreateFile(char *nameOfFile,char *data)
    {
    FILE *file;
    
    if(strchr(nameOfFile,'.') == '\0')
    {
    strcat(nameOfFile,".txt");
    }
    
    file=fopen(nameOfFile,"w");
    
    
    if(ferror(file) != 0)
    {
    fclose(file);
    return 0;
    }
    else
    {
    fprintf(file,"%s",data);
    fclose(file);
    return 1;
    }
    }
    
    int OpenFile(char *nameOfFile,char *data)
    {
    FILE *file;
    
    char c;
    data = (char *) malloc(sizeof(char));
    file=fopen(nameOfFile,"r");
    
    if(file != NULL)
    {
    while((c=getc(file)) != EOF)
    {
        *data=c;
        data++;
        data=(char *)malloc(sizeof(char));
    }
    *data='\0';
    fclose(file);
    return 1;
    }
    else
    {
    *data='\0';
    fclose(file);
    return 0;
    }
    }
    
    
    #endif // FHANDLING_H_INCLUDED
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    First, indent our code! What's the use of using code tags to preserve your code's formatting if you won't even format it to begin with?


    Second, never put code in a header file! What is wrong with you? What did you learn to do such an idiotic thing? Did they teach you that in your school? If so, then get a refund and go find a proper school to enroll in. And report your school to the accredidation authorities.

    Instead, place this code in a source file (.c) and place the prototypes of the functions in the header file, so that other source files can #include that header file and hence know how to call those functions. Then compile all the source files and link their object files together along with the standard library. Most development systems (IDE for "integrated development environment", what many would call "the compiler", even though the compiler is but one part of the IDE) support the creation and maintenance of projects, using that as the mechanism for working with multiple source files.

    If you do not know how to create multi-source projects in your development system, then tell us what you're using so that we can help you.

    Also never place any global variables in a header file, but rather only extern references to globals.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    First, let's format that unreadable mess so that we can see what it's doing:
    Code:
    int OpenFile(char *nameOfFile,char *data)
    {
        FILE *file;
    
        char c;
        data = (char *) malloc(sizeof(char));
        file=fopen(nameOfFile,"r");
    
        if(file != NULL)
        {
            while((c=getc(file)) != EOF)
            {
                *data=c;
                data++;
                data=(char *)malloc(sizeof(char));
            }
            *data='\0';
            fclose(file);
            return 1;
        }
        else
        {
            *data='\0';
            fclose(file);
            return 0;
        }
    }
    Ugh! So many things seriously wrong!

    What's data? When you call OpenFile, what are you passing in there? A char pointer that hasn't been initialized yet? That's what that says to me. So your plan is to malloc some memory, copy the data to it, and pass that malloc'd memory back through the data parameter. Am I correct?

    C only supports passing by value. That means that when you pass an argument to a function, it makes a copy of that value and that is what the function uses. That means that any changes you make to that parameter do not make it back to the argument that was passed; all changes you made cease to exist when you return. So you pass a pointer, let's call it "str", to the function as the argument for the parameter data and you then make several changes to data, assigning it several memory addresses and dropping them just as quickly (memory leaks, which you should have learned about in C++; see below). But then when you return from the function, what is the value of str? Is str pointing to the last address that data pointed to? No, it isn't. Did anything you did to data change str at all? No, it didn't.

    When you want to change the argument that you are passing to a function, then you need to pass the address of that argument, a pointer to the argument. Then the function uses that pointer to change the argument. So instead of being a char*, data needs to be a char**, a pointer to a pointer to char.

    The way you're using malloc is really bad. Every time you call it, you drop the last block of memory that data was pointing to, so that it can never be free'd. This creates a memory leak, one of the most insidious bugs imaginable. In a real-world application that needs to run for extended periods of time, memory leaks will eat away at the heap (the region of memory given to the program to malloc from) until none is left at which point the program will suddenly crash "for no reason". C++ is especially vulnerable to memory leaks, but they can happen in C too.

    Instead, look into the realloc function, which you should use inside the while loop.

    And in the case of file being NULL, meaning that the file didn't open, there's no need to close it since it was never open to begin with. But that's a minor issue.

    What warnings does your compiler give you? From what you've given us, it should at least be complaining about malloc not being defined and about you assigning an int to a pointer, though that cast would hide that problem. Is that why you put that cast in there?
    Last edited by dwise1_aol; April 23rd, 2013 at 03:42 PM.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by jaysinhp
    As student of undergraduate i found C is not having sound functions and facility of string handling as compared to C++.
    Not quite true. Neither C nor C++ has any built-in string type nor built-in string handling capability. It all had to be added in.

    Of course, C came before C++, so the convention of C-style strings came before the same convention was used in C++. That is the convention of using char arrays and of marking the end of the string with a null-terminator. And when we started using C++, we did the exact same thing, only with dynamic strings we would use new and delete instead of malloc and free. Pick up a pre-1998 C++ textbook or look at a C++ program written before and even after that date and you will see C-style strings being used. For that matter, look at iostream's string streams and you will find two, one of which is for C-style strings.

    One common project that every C++ programmer had played with was creating a string class. So the new 1998 standard introduced a string class. It's still not a part of the language, but rather is a class that's been added on.

    Now all C++ instruction ignores the existence of C-style strings, an omission that you have been made to suffer for.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Location
    India
    Posts
    65
    Rep Power
    20
    Thank you for your advice. Well let me be frank i am not from U.S.A. If you can help me to find good educational institute in U.S.A. and what is the procedure to get admission and their websites.
    Else suggest me some good books for bigger except "The C programming language" I have purchased it but due to other exams i will read it in this vacation.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Did you solve your problems? Do you understand how to use header files now?

    For book recommendations, read kathyrollo's recent thread here, C Book Recommendations .

    I am not qualified to make recommendations for schools nor what the procedure is. I got my Computer Science degree at the University of North Dakota and I know that the engineering department had foreign students, but then many other universities also take foreign students and also have a more benign climate. You can go to different universities' websites and look for the information you need.
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Your function OpenFile has a poorly desined interface. It is generally bad practice to hide a malloc() inside a function since it requires the caller to "know" that that memory must be free'd. It is better to pass a data buffer "owned" by the caller, so that the caller decides and knows how to manage memory.

    You are passing a copy of the pointer, the modification of the parameter data in the function will not be reflected in the caller's pointer value.

    If you write a function that opens a file, reads it in its entirety and then closes it, then I suggest that "OpenFile" is an inadequate and misleading name for such a function.

    I would suggest defining the following functions:

    C Code:
    int fileSize( const char* fname )
    {
        FILE* fp = 0 ;
        int size = 0 ;
     
        fp = fopen( fname, "r" ) ;
        if( fp != 0 )
        {
            fseek( fp, 0L, SEEK_END ) ;
            size = ftell( fp ) ;
            fclose( fp ) ;
        }
     
        return( size ) ;
    }
     
    int readFile( const char* fname, char* buffer, int length ) 
    {
        FILE* fp = 0 ;
        int bytes_read = 0 ;
     
        fp = fopen( fname, "r" ) ;
        if( fp != 0 )
        {
            bytes_read = fread( buffer, 1, length, fp )
            fclose( fp ) ;
        }
     
        return( bytes_read ) ;
    }


    Then use it as follows:
    C Code:
     
    int size = fileSize( "myfile" ) ;
    char* buffer = malloc( size ) ;
    size = readFile( "myfile", buffer, size ) ;
     
    ...
     
    // When done with the buffer...
    free( buffer ) ;

IMN logo majestic logo threadwatch logo seochat tools logo