CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: fopen

  1. #1
    Join Date
    Mar 2011
    Posts
    60

    fopen

    Hello,

    Thank you very much for taking time from your busy schedule to help me out. I'm using the following code:

    Code:
    FILE* file;
    file = fopen("logg.txt", "rb");
    int DemonicRainbowUnicorn = sizeof(file);
    file in the debugger gets a Bad Ptr. No matter what file i try to open, the size of the file is always 4. I've tried googling this quite a bit but non of the articles I found helped. Again thank you very much for the help.

    Greg

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: fopen

    file is a pointer to a type FILE. On a 32 bit system, therefore the size of file will always be 4 as pointers are 32 bits. Are you using c or c++? You haven't posted the headers you are using. If you want to know how many bytes there are in the file use
    Code:
    #include <stdio.h>
    
    int main()
    {
    FILE* fp;
    
    long size;
    
    	fp = fopen("debug.txt", "rb");
    	if (fp == NULL) {
    		puts("Problem opening file");
    	} else {
    		fseek(fp, 0, SEEK_END);
    		size = ftell(fp);
    		printf("size is %li\n", size);
    	}
    
    	return 0;
    }
    If you are using c++, I strongly suggest you use the c++ stream classes for i/o.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Mar 2011
    Posts
    60

    Re: fopen

    Thank you very much. I am now reading up on stream classes. I hope you have a great day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured