Thread: reading from a .txt

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27

    reading from a .txt

    hi ,
    i have a text file in the following type:

    1 1 2516 2517 2517 2508
    2 1 2516 2517 2517 2508
    3 1 2516 2517 2517 2508
    4 1 2516 2516 2516 2508


    I want to read it out into my prog to use the values. I have tried something like this...

    Code:
    struct Sensor 
               
    	{
    		int nr,d;
    		double s1,s2,s3,s4;
    
    	};
    
    
    {
    
    struct Sensor s[100];
    int        i;
    
    FILE *fp;
         fp = fopen("Data1.txt", "r");
    
    	 (fscanf(fp, "%d %d %d %d %d %d %d", &s[i].nr, &s[i].d, &s[i].s1, &s[i].s2, &s[i].s3, &s[i].s4) != EOF );
    
    }

    Am getting a runtime error. will be super if i get help :-)

    Regards!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Does fscanf() read in double variables with the same %d type specifier it uses to read in integer's with?

    I think not.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You should use %f or %lf to read doubles, not %d.

    Check if fp is NULL before you try reading from it.

    This should should not even compile (at least if you have your warnings set up at a decent level), so I have no idea how you even got it to crash.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    hm ... i cheanged the %d to %f but still no change... get lesser warnings but it still compiles . But still get the runtime error

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you get warnings, then try to fix them.

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Well firstly, I expect this is a code snippet of some broken for loop, as you've declared an index variable (i) that does not increment/decrement.
    Secondly, the check for fscanf agains EOF looks like you meant it to be part of a conditional statement that never got around to uh.. being conditional...
    Thirdly, and here's the crasher, you specifiy seven arguments for fscanf, yet only provide 6.
    Fourthly, (another potential crasher), you are using %d conversion specifiers for reading in doubles, which should be %lf.
    And fifthly, (yet another crasher), is that if the file "Data1.txt" is not found, then a NULL pointer is returned by fopen.

    I've never seen so many errors in a small snippet of code, and there probably are even more, I just gave up checking after five. Here's my advice: CTRL+A --> DEL. Start again, don't "borrow" code from others, and think through every statement you type, if you are unsure, consult the all-knowing GoogOracle.

    edit: damn you guys are fast!

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    The warnings i am getting now are at some other part of the code. They shouldt afft this part,. The prog worked fine befor i put this new part in , despite of the warnings

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Warnings are sometimes how a compiler says, "You didn't specify what you want, so I'm just gonna go ahead and guess."

    Don't assume the compiler makes the right guess. Many times it means you messed up somehow. It could very well affect something later on, perhaps even causing a crash later on that seems unrelated, although you have enough problems in this code you need to fix.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    Quote Originally Posted by @nthony View Post
    Well firstly, I expect this is a code snippet of some broken for loop, as you've declared an index variable (i) that does not increment/decrement.
    Secondly, the check for fscanf agains EOF looks like you meant it to be part of a conditional statement that never got around to uh.. being conditional...
    Thirdly, and here's the crasher, you specifiy seven arguments for fscanf, yet only provide 6.
    Fourthly, (another potential crasher), you are using %d conversion specifiers for reading in doubles, which should be %lf.
    And fifthly, (yet another crasher), is that if the file "Data1.txt" is not found, then a NULL pointer is returned by fopen.

    I've never seen so many errors in a small snippet of code, and there probably are even more, I just gave up checking after five. Here's my advice: CTRL+A --> DEL. Start again, don't "borrow" code from others, and think through every statement you type, if you are unsure, consult the all-knowing GoogOracle.

    edit: damn you guys are fast!

    wow thatks ...;-) i actually have written this myself :-)
    - i have used "i" as i wanted each line of the text data to be saved under a variable number "i" & i use it further down in my code for calcs.
    - EOF i was not sure how to use it. I actually wanted to get from one line to the next
    - one % f too much i changed it ;-)
    - changed to % d
    - "data1.txt" should always be found. I shuld but include an exception ...

    hm .. the thing still doesnt work

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in scanf %f is for floats
    http://msdn2.microsoft.com/en-us/lib...9d(VS.80).aspx

    use correct format for each type
    note the differences beteween scanf and printf
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    while(fscanf("%d %lf", &intvar, &doublevar) == 2)
    {
       /* we read exactly the amount of fields we wanted - process the input */
    }
    /* eof or error occured - finish reading the file */
    and don't forget to check the fopen return value before using it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    basically it is possible to store the input into structures like how i hav e tried it ?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by sdherzo View Post
    basically it is possible to store the input into structures like how i hav e tried it ?
    yes.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Apr 2007
    Location
    Germany
    Posts
    27
    i think the prog is not able to understand the blank space between two numbers. maybe i schould something else and not fscanf

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I would to it line-by-line fgets() and use sscanf()...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading .txt file
    By vijay85 in forum C Programming
    Replies: 5
    Last Post: 03-09-2009, 04:07 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Need help with reading from .txt
    By Dark_Smily in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2007, 12:56 AM
  4. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 09:23 PM
  5. Reading maps from a .txt file?
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2002, 04:23 AM