reading from a .txt
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...
struct Sensor s;
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 :-)
Does fscanf() read in double variables with the same %d type specifier it uses to read in integer's with?
I think not.
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.
hm ... i cheanged the %d to %f but still no change... get lesser warnings but it still compiles . But still get the runtime error
If you get warnings, then try to fix them.
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!
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
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.
Originally Posted by @nthony
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
in scanf %f is for floats
use correct format for each type
note the differences beteween scanf and printf
and don't forget to check the fopen return value before using it
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 */
basically it is possible to store the input into structures like how i hav e tried it ?
Originally Posted by sdherzo
i think the prog is not able to understand the blank space between two numbers. maybe i schould something else and not fscanf
I would to it line-by-line fgets() and use sscanf()...