Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-10-2006, 11:04 AM   #1 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
read last letter from line

If I have an input file with one word on each line, how do I recognize, it is the last letter of the word in that line.
In other words, is there a tag for End of line like EOF.

Please help. This is the last step in my program.
ron76 is offline   Reply With Quote
Old 07-10-2006, 11:39 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
Read one line at a time, they usualy end with '\n', so fgets() can be used, just make sure to allocate anough room for your line...
Something like:
Code:
#include <stdio.h>
#include <string.h>
#define LINE_LENGTH 1024

int main(int argc, char* argv[]){
    char buffer[LINE_LENGTH +1];
    FILE *fp;
    int i = 1;
    if(argc != 2){
        printf("Usage: %s <file>\n", argv[0]);
        return 0;
    }
    if(!(fp = fopen(argv[1], "r"))){
        printf("Error opening file: %s\n", argv[1]);
        return -1;
    }
    while(fgets(buffer, LINE_LENGTH, fp))
        printf("Last letter besides '\\n' in line %d: %c\n", i++, buffer[strlen(buffer)-2]);
    if(EOF == fclose(fp))
        printf("Error closing file %s\n", argv[1]);
    return 0;
}
Note: the above code has not been tested, it has not been compiled, it was written directly into the composer window without any previus typechecking of any sort.. Your milage may vary.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 07-10-2006, 01:49 PM   #3 (permalink)
ron76
Registered User
 
Join Date: Jul 2006
Posts: 9
ron76 is on a distinguished road
Thanks
ron76 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the line class working, it does not calculate for some reason latin_papi Standard C, C++ 1 05-11-2005 10:57 PM
writting progress to cl on one line sde Linux / BSD / OS X 4 11-08-2004 01:15 PM
getting java to read from a file. philthee Java 6 10-27-2004 08:00 AM
cgi script Henry PHP 25 12-30-2002 06:54 AM


All times are GMT -8. The time now is 07:00 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting