fscanf problem maybe?

This is a discussion on fscanf problem maybe? within the C Programming forums, part of the General Programming Boards category; All right, this is the whole program I'm trying to run, just in case, but I'll explain at the end... ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    fscanf problem maybe?

    All right, this is the whole program I'm trying to run, just in case, but I'll explain at the end...
    Code:
    #include <stdio.h>
    #include <math.h>
    double mel( double x, double y)
    {
      double answer, a;
      a=2;
      answer=a*y;
      answer+=3*x;
      
      return answer;
    }
    
    main ()
    {
         int i, k;      
         double x, y;
         double ben;
         double array_todd[10], jazz[10];
         
         FILE *fout;
         
         fout=fopen("melben.out", "w");
         
         y=2;
         i=-1;
         for (x=1; x<=10; x++){
             i++;
             ben=mel(x,y);
             array_todd[i]=ben;
             }
             for (i=0; i<10; i++) {
             fprintf(fout, "%e\n", array_todd[i]);
             }
             fclose(fout);
             
             fout=fopen("melben.out", "r");
             for (k=0; k<10; k++) {
                 fscanf(fout, "%e\n", &jazz[k]);
                 }
             for (k=0; k<10; k++) {
                 printf("%e\n", jazz[k]);
                 }
                 fclose(fout);
             getchar();
             
    }
    What I'm trying to do is just fprintf to a file, then open it, read it, and print it out again. I'm pretty sure this at least works up to creating the file and opening it again, but something is wrong because it prints out a bunch of ridiculous numbers instead of the one's it's supposed to. I think it might be something to do with my fscanf notation? Can anyone help?
    Thanks.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Are you on Unix/Linux? Why don't you just use the .txt extension? I think .out has something to do with object code and executables.

    for (x=1; x<=10; x++)
    x is double don't use it for counting.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    http://cpwiki.sourceforge.net/Implicit_main
    And array_todd[-1] is undefined.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    30
    The one loop uses x because it's about the value of mel(x,y) at each x, which isn't necessarily and integer (though here it is). Is there a better way to do that?
    (I'm just on windows.)

    And someone told me to use .out so I did. :P But the exact same thing happened with .txt...so I don't think that's the problem.

    And Elysia--what did you mean by
    array_todd[-1] is undefined? Because array_todd starts at [0] so so the index is never [-1].
    I have i start at -1, but it loops to 0 before anything happens...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Linux doesn't care a bit what your file extensions are. I see no problem with "i".

    You're using "w" to write your data. Your compiler has an "environmental variable" which tells it to write out using binary or text mode, when it's not specified.

    Just for fun, try opening and reading the file you've written, with "rb", instead of just "r". I just had a situation where my own compiler wouldn't write a file out in text mode, because it contained structs, (I presume, it never insisted on writing out binary mode before), even though I was requesting it to use "wt" mode.

    Everything worked like a charm, then. I believe the same will be true for your case.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    Quote Originally Posted by zdream8 View Post
    And Elysia--what did you mean by
    array_todd[-1] is undefined? Because array_todd starts at [0] so so the index is never [-1].
    I have i start at -1, but it loops to 0 before anything happens...
    Ah true, true, I missed that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    30
    Adak - I tried "rb" instead of "r" and the same thing still happened... (still the same ridiculous numbers)

    For fscanf should I still be using %e to read it in? Because I tried some other things there and the same thing still happened, which made me think %e was also wrong.

    And Elysia - All right, I just wanted to make sure that's what you were referencing. But I don't think I wrote that in the most clear way. That's just how it ended up. :P

    Thanks.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    Btw, &#37;e is for float, not double. Try using floats instead. I don't know the proper format for double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, I don't like the fout file handle being set to write mode, and then being changed to read mode, with no close and re-open in between.

    I never do that.

    Format for fscanf() for doubles is &#37;lf (el f, not capital i and f).

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    30
    All right, I was thinking something like that might be going on... Do you know where I can find a place that tells the proper formats for all the variables? I was trying to find info about that online, but I couldn't.
    Or does anyone else know?

    I tried the program using floats, and it works perfectly, so that's definitely the problem. I just need to know the notation for doubles for the other program I'm making (this was practice). Again, I'd like to know it for all variables, though, so if anyone knows or knows where to find a chart or something. I couldn't. :S

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    30
    Adak - thanks. It worked with "lf". I tried that earlier, but must have been doing something else wrong, because it didn't work. That's when I got completely lost. lol
    Thanks everyone for your help. (But my above question about the other variables still stands )

    edit - Oh, and I also do have an fclose before I open the file again to read, but did you mean something else? I think it's okay, though. I get the same results opening with a separate program. (For which I was thankful.)

    Why exactly is the notation different for variables with fscanf?
    Last edited by zdream8; 05-24-2008 at 11:06 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    printf("&#37;2f") is for floats, and doubles also.

    I've never used %e, at all.

    Your IDE should have a list of these in the help section. Books have them, common one's in a table usually, and all of them in an appendix.

    Fscanf variable notations are the same as scanf. Printf has some wrinkles all it's own, though. I have no idea why that is, but they are actually very different functions (still, seems like they could have been totally consistent, doesn't it?).

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by Adak View Post
    . Printf has some wrinkles all it's own, though. I have no idea why that is, but they are actually very different functions (still, seems like they could have been totally consistent, doesn't it?).
    No, they couldn't

    scanf gets pointers

    pointer to int is different from pointer to short (you have only 2 bytes to fill instead of 4)
    as well as pointer to float is different from pointer to double for the same reason.

    printf gets actual values, and due to implicit promotion

    char, short and int are promoted to int
    float and double are promoted to double - so no function with var arg list could tell a difference between these... so no different formats make sence here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Adak View Post
    You're using "w" to write your data. Your compiler has an "environmental variable" which tells it to write out using binary or text mode, when it's not specified.
    What is this environmental variable you're talking about?
    Just for fun, try opening and reading the file you've written, with "rb", instead of just "r". I just had a situation where my own compiler wouldn't write a file out in text mode, because it contained structs, (I presume, it never insisted on writing out binary mode before), even though I was requesting it to use "wt" mode.
    "w" and "r" are for text files, not unspecified, and "wt" is a Microsoft extension, AFAIK.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 4
    Last Post: 01-10-2006, 12:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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