Damn newbie error!
Thanks again Matsp, i would not have spotted that!!!
Damn newbie error!
Thanks again Matsp, i would not have spotted that!!!
Sure you would. But it would have taken longer than posting on the forum.
Understanding what you are doing, and what it means is an important part of programming.
I once had the following situation:
However, this is one of those "magical" hardware registers where if you write a one, it clears that bit. I wanted to clear bit 3 [value 8]. And it does that. But what else does it do?Code:*somePtrToHWRegister |= 8;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
i have to say i have absolutely no idea! But you've got me interested!
I'm assuming you're setting the value of that pointer to 8 but what does |= mean?
So is that from a binary point of view? so setting it to 8 would point to 1000 in binary?
I'm only just learning about pointers. I'm an Engineering graduate but regretably never did any programming. (unless you count verilog, verilogA and VHDL) Trying to correct that!
Does a pointer always start with *?
My program now works thanks to the read/write error fix. But i can't figure out how to read more than one line from a file.
I can get fscanf to read the first line but no more,
how would i use fgets or sscanf? and which is better?
Ok, so the |= operator is a short-hand, similar to += or *=.
So, x |= 8 is the same as x = x | 8;
The name of the pointer is just whatever you call it. The star says "get me to what it points to".
Eg.
--Code:int *p; /* Pointer pointing to integer, called "p" */ p = (int *)1000; /* set it to address 1000 - that is not a valid address, but makes a good example */ *p = 8; /* set address contained in p to 8 [that is memory address 1000 will now contain 8]. */
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I think the question was "which is better of fscanf() and fgets() + sscanf()?"
And yes, fgets() with sscanf() gives a better choice if you want to tolerate "badly formed input" [eg letters where digits are expected].
To read multiple data from a file, you need some form of loop. If you know how many elements before you start reading [or by reading some form of header/first element or such], you can use a for-loop [preferrably with a sanity check to see if the data is actually being read correctly]. If the amount of data is "unknown", then you would need to use detection for "no more data", such as "while(fgets(...) != NULL)" or "while(fscanf(...) != EOF)".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ok i'm having a lot of trouble with this so maybe it would help if i could explain what i'm trying to do.
If i have a file called input_file
whose contents look like this:
Thats 9 rows by 9 columns.Code:123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
i want to read it in to an array such as array[9][9].
I can write it to an output file using fprintf.
How would i go about doing that?
Thanks, i think i'm slowly getting the grasp of this. Even though i should actually be working!