Thread: Reading the First Character of the Last Line

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    68

    Reading the First Character of the Last Line

    Hi, all

    I have a question regarding reading files

    Once I get the code to open the file how can I get it to scan right to the first character of the last line of the .txt file?

    thanx
    -Ti22-

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The only portable way is to read the file until you get to the last line:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      char buf[BUFSIZ];
      FILE *in = fopen ( "test.txt", "r" );
    
      if ( in == NULL )
        return EXIT_FAILURE;
      while ( fgets ( buf, sizeof buf, in ) != NULL )
        ;
      printf ( "The last line is \"%s\"\n", buf );
      printf ( "The first character of the last line is '%c'\n", buf[0] );
    
      return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  4. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM
  5. Reading a line from a txt file
    By Nippashish in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2002, 06:22 PM