Thread: clarification with newline character

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    clarification with newline character

    Hi all,

    According to the definition of fgets
    Code:
    char * fgets ( char * str, int num, FILE * stream );
    "A newline character makes fgets stop reading". so my doubt is
    is it possible that in the file a newline character apart from the last new line can occur?
    Code:
    char str[50]= "a newline character in between \n is it possible\n"
    how the situation should be handled?

    Thanks and regards,
    Satya

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > is it possible that in the file a newline character apart from the last new line can occur?
    No.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    is it possible that in the file a newline character apart from the last new line can occur?
    Of course. It happens all the time. For example:
    Code:
    this
    is a
    sentence
    spread out over
    several lines.
    Quote Originally Posted by Satya
    how the situation should be handled?
    You read one line, then another line, then another line...
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    my doubt is in a file in a single line if i forcibly put a character like '\n'
    Ex: this is satya \n learning c\n ---------line 1
    this is line 2 \n ---------line 2 and so on.

    then in the first line i have two \n characters first one was put by me, then how should i read back the complete line containing two new lines.
    the scenario is it possible?

    Thanks and regards,
    satya

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    my doubt is in a file in a single line if i forcibly put a character like '\n'
    Ah, I understand your confusion now. Let us assume that the newline sequence is the newline character. This means that in the file, there is effectively only one "line". The lines that you understand to be lines are only because there is a newline character separating them.

    In other words, my example file really is equivalent to:
    Code:
    char text[] = "this\nis a\nsentence\nspread out over\nseveral lines."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    hi,

    Thank you for the replies. i was just trying to understand how the file data will be stored in the computer. Because if i write a program like this
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        FILE *fp;
        char str[50];
    
        fp = fopen("out.txt","r");
    
        if(fp == NULL)
        {
            printf("error opening file\n");
            return 0;
        }
        else
        {
            while(fgets(str,40,fp) != NULL)
            {
                printf("%s",str);
            }
        }
    
    return 0;
    }
    and the out.txt file contains the following text as attached. The output is the same as out.txt with the '\n' character
    Code:
    hai this is \n satya
    i am fine here
    compared to the previous output which printed on the new line.
    so it basically handles the file data differently compared to a string data, can you direct or give a link how it will store because if you give as a string it prints in the new line whereas if you give it in a file it just prints '\n'. But thanks for the quick replies.

    Thanks and regards,
    Satya
    Attached Files Attached Files

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    and the out.txt file contains the following text as attached. The output is the same as out.txt with the '\n' character
    '\n' is one character. What you are looking at is the string which we write in C as "\\n", i.e., a backslash character followed by 'n'. This is why you get "\n" printed as you see it. It is not a newline character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    thank you laserlight, yeah i got it.

    thanks and regards,
    satya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-12-2010, 12:54 PM
  2. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 07:50 PM
  3. Newline character
    By sean in forum Networking/Device Communication
    Replies: 6
    Last Post: 11-24-2004, 03:33 PM
  4. comparing int to newline character
    By RedZippo in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2004, 06:37 PM
  5. character pointer clarification
    By theweirdo in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 10:51 AM