returning file pointer

This is a discussion on returning file pointer within the C Programming forums, part of the General Programming Boards category; Code: FILE* open(const char filename[]) { FILE* fp; char c[10]; printf(filename); fp = fopen(filename, "r"); if (fp == NULL) { ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    returning file pointer

    Code:
    FILE* open(const char filename[]) {
          FILE* fp;
          char c[10];
          printf(filename);
          fp = fopen(filename, "r");
          if (fp == NULL) {
             cout << " cannot open ";
          }
          else {
             cout << " opened\n";
          } 
          return fp; 
    }
    i need to make it like if i doesnt equal NULL it returns the pointer but if i does equal NULL i need to return false

    also i can only use one exit (one return)

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Returning NULL pretty much means returning false for pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since you're using cout, this isn't C code, so it should be in the C++ forum.

    If you want to return false, you need to return a bool instead of a FILE*, but what's wrong with just returning NULL?

    BTW, since this is C++ code, using an fstream instead of fopen() would be more object oriented...

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ya thats true, i think i just made it seem hard but can someone check out this function and tell me if there is anything wrong because i get a "Segmentation fault":
    Code:
    int valid(const char* str){
        //char f[12];
        int k[10], d[10], sum=0, ret=0, lenght = strlen(str);
        if (strcmp(str, "0") == 0) {
                    ret = 0;
                }
        if (strcmp(str, "") == 0) {
                    ret = 0;
                }
        if (str == NULL) {
                    ret = 0;
                }
        if (lenght != 10) {
                    ret = 0;
                }
        //strcpy(f, str);
        for(int e=0;e<10;e++){
                k[e] = str[e];
                k[e] = k[e] - 48;
        }
        d[0] = k[0] * 10;
        d[1] = k[1] * 9;
        d[2] = k[2] * 8;
        d[3] = k[3] * 7;
        d[4] = k[4] * 6;
        d[5] = k[5] * 5;
        d[6] = k[6] * 4;
        d[7] = k[7] * 3;
        d[8] = k[8] * 2;
        d[9] = k[9] * 1;
        sum = (d[0] + d[1] + d[2] + d[3] + d[4] + d[5] + d[6] + d[7] + d[8] + d[9]) &#37; 11;
        if(sum==0){
                   ret = 1;
                   }
        else{
                   ret = 0;
                   }
        
        return ret;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >tell me if there is anything wrong because i get a "Segmentation fault":

    (1) You do several checks, and if you find an error, you don't return from the function. So you either need to return from the function once an error is found (return ret) , or use an else if() structure.

    (2) The check for a null pointer should be first, not third. Otherwise str isn't pointing to a string.

    (3)
    > k[e] = k[e] - 48;
    Simpler and more readable is:
    k[e] = k[e] - '0';

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    i made the changes
    Code:
    int valid(const char* str){
        int k[10], d[10], sum=0, ret=0, lenght = strlen(str);
        if (str == NULL) {
                    ret = 0;
        }
        else if (strcmp(str, "") == 0) {
                    ret = 0;
        }
        else if (lenght != 10) {
                    ret = 0;
        }
        else {
             for(int e=0;e<10;e++){
                k[e] = str[e];
                k[e] = k[e] - '0';
             }
             d[0] = k[0] * 10;
             d[1] = k[1] * 9;
             d[2] = k[2] * 8;
             d[3] = k[3] * 7;
             d[4] = k[4] * 6;
             d[5] = k[5] * 5;
             d[6] = k[6] * 4;
             d[7] = k[7] * 3;
             d[8] = k[8] * 2;
             d[9] = k[9] * 1;
             sum = (d[0] + d[1] + d[2] + d[3] + d[4] + d[5] + d[6] + d[7] + d[8] + d[9]) % 11;
             if(sum==0){
                   ret = 1;
             }
             else{
                   ret = 0;
             }
        }
        return ret;
    }
    but i get the same segmentation fault

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int k[10], d[10], sum=0, ret=0, lenght = strlen(str);
    strlen() doesn't check for a null pointer, so somehow setting length to strlen(str) must come after the null pointer check. Since you actually only use the length once, you could forget about storing the value and change:
    Code:
    >    else if (lenght != 10) {
    To:
    Code:
        else if (strlen(str) != 10) {
    Or you could change it to:
    Code:
        else {
            lenght = strlen(str);
            if (lenght != 10) {

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Come to think of it, you could change it to:
    Code:
        else if ((length = strlen(str)) != 10) {

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    ya its working now, i used the last method works perfect now man thanks

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i used the last method
    I liked that one too. It's in the spriit of the C language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Problems with file pointer in functions
    By willie in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 12:54 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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