Thread: Read/Write long integer to file!

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Read/Write long integer to file!

    Hi!
    I have a very strange problem! When I save long integer to textfile, everything works well, but when I try to read it, I get completely different value.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(){
       	
    	long long_int;
    	
    	FILE *fd = fopen("file", "w");
    
    		fprintf(fd, "%li\n", 245749955);
    
    	fclose(fd);
    
    	FILE *fd2 = fopen("file", "r");
    
    		fscanf(fd2, "%li\n", long_int);
    		printf("%li\n", long_int); // OUTPUT IS -1073744252, BUT IT SHOULD BE 245749955
    
    	fclose(fd2); 
    
    return 0;
    }
    What is wrong? Thank you!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You forgot the & in the fscanf function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean to say: "You forgot to pay attention to your compiler."


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The compiler probably would not have caught that error because it doesn't parse the format flags to see what parameters should be passed.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Thanks!
    You mean to say: "You forgot to pay attention to your compiler."
    No errors or warnings

  6. #6
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > What is wrong?
    It's not written in C - check your use of embedded declarations
    You don't check errors
    You don't realise that 245749955 is OUTSIDE the range of your long integers - try unsigned

    > No errors or warnings
    Get gcc (or a port such as dev-c++)
    Then discover the delights of
    gcc -W -Wall -ansi -pedantic -O2 mycode.c

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One of your problems is this:
    Code:
    fscanf(fd2, "%li\n", long_int);
    Think back to how you use scanf().

    You might want to check the return value of fopen() to see if it failed. Other than that, try using 100 instead of 245749955.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ancient Dragon
    The compiler probably would not have caught that error because it doesn't parse the format flags to see what parameters should be passed.
    It doesn't have to. All arguments to fscanf are pointers. An integer is not a pointer, long or no.
    Code:
    #include<stdio.h>
    int main( void )
    {
        FILE *fp = NULL;
        int x;
        fscanf(fp,"%d",x);
        
        return 0;
    }
    
    fscanfargs.c: In function `main':
    fscanfargs.c:6: warning: format argument is not a pointer (arg 3)
    Some of us actually do know what we're talking about here.

    [edit]
    Yes, that's intentionally NULL, no it doesn't have anything to do with the warning. It simply serves as an illustration to get the correct warning text without me having to make some file.
    [/edit]


    Quzah.
    Last edited by quzah; 11-02-2005 at 12:05 PM. Reason: Answering your question before you ask it.
    Hope is the first step on the road to disappointment.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could have used stdin.

    You don't realise that 245749955 is OUTSIDE the range of your long integers - try unsigned
    245749955 would easily fit into a signed int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not on some piece of crap 16 bit compiler it won't. Unsigned won't help either. (Who'll bet me it's TC 3.0? ... Yeah, it's probably not, but you never know with people around here. So we have to be careful on our generalizations.)


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I never though of that. But at the very least, it would fit into a long, which is what he has.

    Who'll bet me it's TC 3.0?
    Do you mean TC 2? That's the one that's freely downloadable.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by quzah
    It doesn't have to. All arguments to fscanf are pointers. An integer is not a pointer, long or no.
    fscanf takes a variable number of parameters and is prototyped like below, which prevents the compiler from doing type checking of the parameters beyond the first two:
    Code:
    int __cdecl fscanf(FILE *, const char *, ...);
    Last edited by Ancient Dragon; 11-02-2005 at 12:22 PM.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, but some compilers know better. They know what scanf() is supposed to take for arguments.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Yeah, but some compilers know better. They know what scanf() is supposed to take for arguments.
    Not based upon any prototype they don't. scanf() is prototyped the same way. The only way I can think of for those compilers to know the data types of the parameters is to parse the format specification string. Or it might be hard-coded into the compiler that if parsing functions scanf() or fscanf() then all parameters must be pointers.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't matter if it has an unspecified number of arguments, they're still all pointers. How the hell is scanf supposed to updated the contents of a variable passed to it, so it takes effect in the calling function, without using a pointer?

    Yeah, so anyway, as I said, it always takes pointers for its arguments.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM