Thread: Two questions. (Sorry for lots of them =( )

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    Two questions. (Sorry for lots of them =( )

    Hello,
    First of all I am sorry for my recent posts. I know I have sent a lot. But I couldnt think all questions at the same time.I hope you do not mind.


    I was wondering ; for example I have a txt file which looks like :

    Hello everyone 24 how are you
    Nice...


    When I use this line and take the whole line to my string :

    >fgets(s,80,fpointer);

    String becomes: Hello everyone 24 how are you

    And it has a '\0' at the end. Not NULL . Why not NULL. What is the exact differnce between null and \0. I know \0 is a character. But still could not get it trully. Secondly , It adds a \n character at the end of the string. Why it does that ? DOes txt file has \n character at the end of each line?



    --------------------------------------------
    I have one more question :

    I am still regardin the txt file I have mentioned above.

    When I use :
    >fscanf(fpointer,"%d",&integer);

    I can not assign "24" to my integer. It is still like 248239239 . Why is that?

    Thank you for all responses.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The end of a string is not NULL - NULL, when it is used, is for comparing with pointers that point to nothing, such as:

    Code:
       char *p;
       p = strchr(somestring, somechar);
       if (p == NULL)
          ... didn't find somechar in somestring. ...
    The zero at the end of a string is SOMETIMES called 'NUL'. There is a FAQ for "What's the difference between NUL and NULL?" or some such.

    Essentially, both are zero, but one is a char, the other is a pointer (or something that can be compared to a pointer).

    --
    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. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Other questions ? =)
    I will also ask why do we need to use pointers for structs when it is adequate to define standart struct variables?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As to your fscanf() question - fscanf will skip whitespaces until the first non-whitespace. When it hits that, it will TRY to read that as a number. Since the first non-whitespace in your text is not a number, your number never gets set - it happens to be whatever was in the variable before, and by the looks of the value, it was a local variable that wasn't given a value, so it contains some "large sort of random number".

    scanf() and related functions are quite "picky" on the format of the input, and can easily be confused by bad input.

    Check the return value from scanf, and I bet you it will be zero in your example, because it failed to read a number.

    --
    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. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    Other questions ? =)
    I will also ask why do we need to use pointers for structs when it is adequate to define standart struct variables?
    Eh? You need pointers to structs when you want to pass or keep track of a structure without making a copy of the structure itself. Pointers to structures are also useful when you don't know at programming time how many you need, and you want to dynamically create space for the structures using malloc.

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

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Yea I got it , but I can not imagine the background of working of pointer which keeps a struct. Which adress does it show? I dont think that a struct without a normal variable or pointer variable has an adress. And a pointer gotta point somewhere. Where does it point? =)

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Also , I would like to ask my former questiom above :

    It adds a \n character at the end of the string. Why it does that ? DOes txt file has \n character at the end of each line?

    what about it?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, every line as a newline on the end. Some input functions (such as fgets()) will include the newline in the input [it is useful to see if you got the whole line or not].

    Technically, the "newline" in a system is dependant on which system the file is on, in Windows (and DOS, OS/2, CP/M and several others) the newline is actually CR/LF, whilst in Unix, it's only LF, and in Apple's older OS's it was only CR.

    CR stands for "Carriage Return", and comes from the old "typewriter" style teletype terminals that existed in the 1950-1970 timeframe - it means "return the typing carriage to the left-hand side of the paper".

    LF stands for "LineFeed", which is easier to follow: push the paper forward one line.

    Fortunately for us, as long as you use text-mode, all these differences are hidden behind the scenes, so the string you read will have a newline (LF) at the end, whether it is a Windows, Linux or some other OS you're running on.

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

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Is the samething true about fscanf for dat files? Because in a book , I see people reading integers with fscanf , but it doesnt work for me..

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    Is the samething true about fscanf for dat files? Because in a book , I see people reading integers with fscanf , but it doesnt work for me..
    Ehm ".dat" files aren't a very defined format - .dat just means that it's some for of data - it could be plain text, quoted text, XML or any form of binary data for all I know, and of course, each of those file-formats require a different type of read function.

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

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I AM Sorruy I figured it out . That was a silly question anyway , thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM