Thread: Copy a binary file to a 2d array? What am I doing wrong??

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Copy a binary file to a 2d array? What am I doing wrong??

    Code:
    #include<stdio.h> 
    
    
    void main()
    {
        int x=0;
        int y=0;
        char c;
    FILE* HUG;
    FILE*fopen();
        
    HUG=fopen("c:\\beat.bmp","rb"); 
        char array[600][480];
        c=getc(HUG);
    
        while(c!=EOF) 
        {
        
        
        array[x][y]=c;
        y++;
        c=getc(HUG);
    
        if(y==480)
        {
        
        x++;
        y=0;
        
        }
        
        
        }
        fclose(HUG);
    }

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    I keep getting these errors no matter what I do...

    error C2143: syntax error : missing ';' before 'type'
    error C2065: 'array' : undeclared identifier
    error C2109: subscript requires array or pointer type
    Last edited by noethe; 06-12-2013 at 04:48 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your indentation is awful. Please post more readable code next time. Read this: http://sourceforge.net/apps/mediawik...le=Indentation

    Line numbers with those errors would really help. When I compile the code you posted, I get the following errors:
    Code:
    gcc -Wall -Werror -ggdb3 -std=c99 -o bar bar.c
    bar.c:4:6: error: return type of ‘main’ is not ‘int’ [-Werror=main]
    bar.c: In function ‘main’:
    bar.c:13:8: error: variable ‘array’ set but not used [-Werror=unused-but-set-variable]
    Are you sure the code you posted is identical to the code that produced those errors? If made changes and didn't save them, the compiler could be using an old version of code.

    Anyway, I don't see anything in there that would cause the errors you posted. As for the errors I got:
    line 4: It's int main, not void main, and make sure you return a value at the end, 0 is the convention for success. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    line 13: You assign values to array, but never do anything with the values. I assume that's because this code is not complete.

    Also, the following line is not necessary since the prototype for fopen is in stdio.h. Remove it.
    Code:
    FILE*fopen();

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You also need to make sure you don't overrun your array. What happens when x reaches 600?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Thanks for the reply.

    Yeah, the code is incomplete because this was my first step. But yes I keep getting these same errors no matter what and I'm going crazy. Could it be my compiler ?? I don't know I've stripped the code down to just this section and even started a new project so the compiler would only use that code but still...this

    c(13): error C2143: syntax error : missing ';' before 'type'
    c(20): error C2065: 'array' : undeclared identifier
    c(20): error C2109: subscript requires array or pointer type

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Good catch by bithub, you should check to ensure x doesn't exceed 600. Note, instead of using a literal 600, define constants
    Code:
    #define MAX_X    600
    #define MAX_Y    480
    Then, if you want to change your code to work with different size images, it's only two small changes in one place, instead of dozens in several places.

    It could be your compiler, but not in the "it's a bug" sense, rather the "you're compiling with the wrong settings" sense. What compiler are you using? It doesn't look like GCC, so I can't be of much help.

    Did you fix the declaration of main like I suggested, add a return value and remove the unnecessary line? If not, do so. It probably wont fix the problem, but it ought to be done anyway. Also, try moving the definition of array up with the other variables. It's possible your compiler doesn't like mixed statements and declarations.

    Lastly, you need to check the return value of fopen to make sure it's not NULL before you proceed. No point in trying to read from a file you can't open. Add this immediately after the fopen call:
    Code:
    if (HUG == NULL) {
        perror("fopen");
        return EXIT_FAILURE;
    }

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You might be using a very strict C89 compiler. What happens if you move the declaration of array above the call to fopen()?

    Also, you need to declare c as an int to properly compare it to EOF.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by noethe View Post
    I keep getting these errors no matter what I do...
    As bithub mentioned, in C, you need to declare all variables at the start of a function. In this case, the error is caused by the fact that you declared char array[][] after the call to fopen. Move that line up to where you declare the other variable and the error(s) should go away.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The error format is what "Microsoft Visual C++" uses which is not a C99 compiler.

    As far as I know, "Microsoft Visual C++", as a C compiler, doesn't even have the option of "mixed declaration".

    Soma

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by rcgldr View Post
    In C, you need to declare all variables at the start of a function. In this case, the error is caused by the fact that you declared char array[][] after the call to fopen. Move that line up to where you declare the other variable and the error(s) should go away.
    Only true for C89 and older. C99 and later allow mixed declarations.

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    Yes, I changed void main to int main, removed the extra line, added a return value,changed c to type int instead of char, and added the code you suggested. I'm using Microsoft studio express 2010.

    You guys rock! I moved the declaration to the top of the program and those errors just disappeared! I've been just going crazy trying to figure this out and you guys really helped( wipes tear from eye) .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-23-2010, 12:43 AM
  2. binary file copy has extra character
    By leonv in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 11:14 PM
  3. copy two file with binary method
    By arastoo.s in forum C Programming
    Replies: 10
    Last Post: 02-26-2010, 02:30 PM
  4. creating a copy of binary file
    By aarti_gehani in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 03:49 AM
  5. Large Binary File Copy
    By Robert Austin in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 07:57 AM