fscanf problem in C

This is a discussion on fscanf problem in C within the C Programming forums, part of the General Programming Boards category; Hi, I have a programing problem regarding fscanf. I have a text file that looks like this: JPL.405 2400000.00000 2400000.00000 ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    fscanf problem in C

    Hi,

    I have a programing problem regarding fscanf.

    I have a text file that looks like this:

    JPL.405
    2400000.00000
    2400000.00000
    1
    1
    1
    1
    1
    1

    the first item is always a string, and the other ones can vary - there can be even more values of different type. The problem is that I must read each one at a time - not all together with a while statement or the usual syntax of fscanf.
    So I tryed:

    fscanf(file_in, "%s", InFile);
    fscanf(file_in, "%lf", JD);
    etc...

    It didn't work - it just read the first one. How can I do this using fscanf ?

    Please anwer as soon as possible.

    Best regards,

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fscanf(file_in, "%lf", JD);
    I presume JD is a double? So use:
    >>fscanf(file_in, "%lf", &JD);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    5

    it works

    Hi again,

    It really works if I use the & in all the calls.

    Thank you very much

    Best regards

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: it works

    >>It really works if I use the & in all the calls.
    When you say "all the calls", you don't need to use & on a char array variable. For example, this is the correct way:

    char buf[100];
    int i;
    int *ip = &i;
    fscanf(fp, "%s", buf);
    fscanf(fp, "%d", &i);
    fscanf(fp, "%d", ip);

    To be more specific, you have to pass the address of a variable (or pointer). As array names are pointers in disguise, you don't need the & on them.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User JamesM's Avatar
    Join Date
    Sep 2003
    Posts
    18

    I'm having a problem with this too.

    The following code works:

    fscanf(datafile, "%s", temp2);
    teams[0].teamname = temp2;

    However, these don't. Any ideas why?

    fscanf(datafile, "%s", teams[0].teamname);

    or

    fscanf(datafile, "%s", teams[temp].teamname);

    or

    fscanf(datafile, "%s", (teams[0].teamname));

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    However, these don't. Any ideas why?
    When you use scanf (and any variants - fscanf, sscanf) for reading into variables, the result of any conversion is stored directly at the memory address given for that variable. Thus, if data (such as characters) are to be stored at a given address, there needs to be adequate allocated memory for that space.

    I imagine then that with the first code:
    Code:
    fscanf(datafile, "%s", temp2);
    that temp2 is declared as an adequately sized array and is thus able to store the input string correctly. (Either that, or you just got lucky and didn't over-write anything important).

    You then assign a pointer to point to that array:
    Code:
    teams[0].teamname = temp2;
    Your next sections of code are actually trying to do something different. They are trying to store the read-in string directly at the pointer address (rather than make it point to the read-in string as done earlier). This is causing problems because the memory allocated for a pointer is smaller than what you are trying to store.

    Cheers,
    Neil.
    Beware the fury of a patient man.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    JamesM,

    Since you are using a structure, rather than define teamname in the structure as char *teamname, instead make it char teamname[n], where n is an adequate size to store the largest team name.

    It is usually always better to have self contained structures rather than have members pointing to external memory. Using the recommended approach, any of the later mentioned attempts would work (The first would not however - You would get a compile error. Instead, you would need to use strcpy or something similar)

    Cheers,
    Neil.
    Beware the fury of a patient man.

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