How do you get the newline in a file?

This is a discussion on How do you get the newline in a file? within the C Programming forums, part of the General Programming Boards category; i got a program thats get the newline... i am able to get it vertical but im looking for the ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    19

    How do you get the newline in a file?

    i got a program thats get the newline...
    i am able to get it vertical but im looking for the line to be horizontal.

    so lets say my text has "this is an example \n = 1st line "
    "i'm so crazy about c \n = 2st line"

    my program only gets ti...first letter of 1st line and second line.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        char *filename = "room.txt";
        FILE *infile;
        char line_buffer[100]; 
        char newline;
    
        
        infile = fopen(filename, "r");
        if (!infile) {
            printf("Couldn't open file %s for reading.\n", filename);
            return 0;
        }
        printf("Opened file %s for reading.\n", filename);
        
       newline= 0;
        while (fgets(line_buffer, sizeof(line_buffer), infile)) {
            ++newline;
    
            if(newline= 1)
        {
            
            printf("%c", line_buffer[0]);
    
        }
        }
    
    fclose(infile);
        return 0;
    }
    Last edited by Tryhard; 03-03-2013 at 10:17 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    51
    Post the output of your program in code tags.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    TEXT FILE CONTENT

    this is an example
    i'm crazy about c program
    so thats why i'm learning it


    Code:
    output 
    ./a.out
    Opened file test.txt for reading.
    t
    i
    s

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Code:
    if( newline= 1)
    This is a logical error - you are assigning the value 1 to newline. Equality operator is '=='.

    Other than that, I'm unclear as to what you're trying to accomplish.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > printf("%c", line_buffer[0]);
    Perhaps you meant

    printf("%s", line_buffer);

    You only get the first letter of each line, because that's all you asked for.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    thanks, its working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convertfen.c:61:1: warning: no newline at end of file = ???
    By Adam Rinkleff in forum C Programming
    Replies: 5
    Last Post: 06-23-2011, 05:40 AM
  2. Replies: 3
    Last Post: 02-26-2009, 11:24 AM
  3. Newline writing to a file
    By automagician in forum C Programming
    Replies: 7
    Last Post: 05-08-2005, 01:22 AM
  4. how to detect newline in a *txt file
    By lwong in forum C Programming
    Replies: 3
    Last Post: 10-19-2003, 10:52 PM
  5. Reading data from a file till there is a newline?
    By mackol in forum C Programming
    Replies: 18
    Last Post: 06-27-2002, 10:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21