Thread: Scanf ruining my problem!

  1. #16
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Damn newbie error!
    Thanks again Matsp, i would not have spotted that!!!

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AmbliKai View Post
    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:
    Code:
       *somePtrToHWRegister |=  8;
    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?

    --
    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.

  3. #18
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    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?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #20
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by AmbliKai View Post
    I can get fscanf to read the first line but no more,

    how would i use fgets or sscanf? and which is better?
    The question should not be which is better ( fgets or sscanf ).
    The combination of fgets ( to read a line of input) and sscanf ( to extract the values from that line ) is better then using fscanf. It makes it a lot easier to detect and recover from bad input.
    Kurt

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #22
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    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:

    Code:
    123456789
    123456789
    123456789
    123456789
    123456789
    123456789
    123456789
    123456789
    123456789
    Thats 9 rows by 9 columns.

    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM