View Single Post
Old 07-10-2006, 12:39 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
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