Thread: How do I add char array to another array?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    24

    How do I add char array to another array?

    Code:
    const void *convToChar(const char *file_path, unsigned int char_length, unsigned int line_length){
        FILE *file;
        char f_text[char_length + 1];
        char text[char_length + 1];
        file = fopen(file_path, "r");
    
    
        for(unsigned int i = 0; i < line_length; i++){
            fgets(f_text, char_length + 1, file);
            text += f_text;
        }
    
    
        const char *p_text = text;
        fclose(file);
    
    
        printf("%s", text);
    
    
        return p_text;
    }

    Above code is for OpenGL shader source function. The shader file needs to be converted from shader file to a char array.



    This is where i'm stuck,

    Code:
        for(unsigned int i = 0; i < line_length; i++){
            fgets(f_text, char_length + 1, file);
            text += f_text;
        }
    text += f_text;

    It says "invalid operands to binary + (have 'char[(sizetype)(char_length + 1u)]' and 'char *')"


    fgets gives me the current line of the file but I need a function that stores my whole file into a big char array. Is there a function or a way to solve my problem?
    Last edited by yj1214; 08-12-2015 at 10:11 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can either read into the destination array directly, or copy from the array that stores the input into the destination array.

    The former would be more efficient but the latter would be easier to handle if you want to recover from a read failure.

    That said, you have two problems:
    • f_text and text have the same number of chars, so they can store the same maximum amount of data. Therefore, if text already has a portion in use, and f_text is fully used, you cannot fully append the contents of f_text to the contents of text without writing out of bounds.
    • At the end, you return p_text. p_text points to the start of text, but text is a local array. Therefore, this means that you are returning a pointer to a local non-static object. This is problematic because by the time control returns to the caller, this object no longer exists.


    Since you "need a function that stores (your) whole file into a big char array", perhaps you should consider if using dynamic memory allocation with malloc/realloc/free would be appropriate here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2015
    Posts
    24
    I think I almost got it but I have a small glitch in my program.



    Code:
    int main(){
        char *sentences = convToChar("tfile.txt", 96, 15);
        printf("%s", sentences);
    
    
        return 0;
    }
    
    
    char *convToChar(const char *file_path, unsigned int char_length, unsigned int line_length){
        FILE *file;
        char f_text[char_length + 1];
        //char text[char_length + 1];
        char *text = malloc(sizeof(char) * (char_length + 1));
        file = fopen(file_path, "r");
    
    
        for(unsigned int i = 0; i < line_length; i++){
            fgets(f_text, char_length + 1, file);
            strcat(text, f_text);
        }
    
    
        fclose(file);
    
    
        return text;
    }
    When I run this program, it prints out my text file but with the weird symbols like below.

    "╪%UHello!"


    But when I use printf("%s", text); in my convToChar,

    Code:
    //stuffs
    //stuffs
        fclose(file);
    
            printf("%s", text);
    
    
        return text;
    }

    This prints out the actual text file...


    So I think there's something wrong in the main,

    Code:
    char *sentences = convToChar("tfile.txt", 96, 15);
        printf("%s", sentences);
    Am I doing something wrong?
    Last edited by yj1214; 08-13-2015 at 12:43 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is the shader file in a binary format or text? At the moment you are treating your test file content as text, but if that is not the case for the actual file, then using fgets to read and printf with %s to write would be wrong.

    There are other things to note:
    • Remember to include the appropriate headers and to declare convToChar before calling it in main.
    • Where did you get the magic numbers 96 and 15 from? What happens if a line is longer than expected, or if the entire file has more characters than expected?
    • Although you used malloc, you did not use realloc to allocate more space when needed, and then you did not call free in main. You also did not check that malloc did not return a null pointer.
    • You should check that fopen did not return a null pointer.
    • Although strcat could work if your file format is text, when run in a loop like this it is inefficient. You should keep track of where the end of the string is and concatenate by copying directly starting from that point.
    • sizeof(char) == 1 is always true.


    By the way, note that this declares a variable length array:
    Code:
    char f_text[char_length + 1];
    It is not wrong, but there's the caveat that the variable length array feature only became standard C in C99, then it became optional in C11.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    As laserlight suggest your best option is to copy the line read direct onto destination array. However you may end up with a problem where the destination array wont have enough memory to hold all of your text in it. One way to resolve this would be to open the file and then calculate the size of the file using fseek until the end of the file. Having calculated the size. Allocate enough memory using malloc and use fgets to read text direct to the destination array.

    ~H
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

    JIm

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by yj1214 View Post
    Code:
    char *convToChar(const char *file_path, unsigned int char_length, unsigned int line_length){
        FILE *file;
        char f_text[char_length + 1];
        //char text[char_length + 1];
        char *text = malloc(sizeof(char) * (char_length + 1));
        file = fopen(file_path, "r");
    
    
        for(unsigned int i = 0; i < line_length; i++){
            fgets(f_text, char_length + 1, file);
            strcat(text, f_text);
        }
    
    
        fclose(file);
    
    
        return text;
    }
    The contents of the memory pointed to by text is uninitialized. You need to at least set the first element to a null character before you use it:
    Code:
    text[0] = '\0';
    That line ensures that it's a valid string, which strcat expects.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2014, 06:12 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  4. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  5. Replies: 3
    Last Post: 11-17-2008, 12:36 PM