Thread: How do I read last n characters of a string in a single file?

  1. #1
    Registered User
    Join Date
    Dec 2017

    How do I read last n characters of a string in a single file?

    I want to read the last n characters of a line in the file "passwordhashes.txt". If "$1$$" is detected in a line, I want to print the last 26 characters of that line and if "$6$$" is detected instead, I want to print the last 90 characters of that line. The following code returns the first 26 characters if md5 is detected and first 90 characters if sha512 is detected but that is not what I want.

    #include <stdlib.h>
            #include <stdio.h>
            #include <string.h>
            #include <strings.h>
            int printDataError(){
              printf("Data error: Invalid entry found (Skipped)\n");
            int main(void)
                    char buf[1024];
                    char **arr2 = NULL;
                    int size2 = 0;
                    FILE *f2;
                    f2 = fopen("passwordhashes.txt", "r");
                    while(fgets(buf, 1024, f2))
                        arr2 = realloc(arr2, sizeof(char*) * size2);
                        arr2[size2 - 1] = strdup(buf);
                char line[1000];
                   char hash[1000];
                   char md5[5]= "$1$$";
                   char sha512[5]="$6$$";
                  char *ret;
                  char *ret2;
               //Read file line by line     
               for(int j = 0; j < size2; j++){
                memset(hash, '\0', sizeof(hash));
                strcpy(line, arr2[j]);
                //Search for md5 
                  ret = strstr(line, md5);
                //Search for sha512
                  ret2 = strstr(line, sha512);
                if (ret){
                   strncpy(hash, line,26);
                   printf("Line %d hash: %s\n", j+1,hash);
                else if (ret2){
                  strncpy(hash, line,90);
                   printf("Line %d hash: %s\n", j+1,hash);
                    return 0;

    A few lines in the passwordhashes.txt (there are a total of 219128 lines in this file so I will just list out a few):


  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    I think you are close it is just a matter of adjusting the line pointer.

    strncpy(hash, line + strlen(line) - 26 - 1, 27);

    strncpy(hash, line + strlen - 90 - 1, 91);

    This should work for each respective case. The extra plus or minus 1 in the arguments is to make sure that the data you print doesn't really count \n. It may be in the hash string nontheless, but you will see 26/90 characters that matter.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Indentation style - Wikipedia
    Your code has none, and is a eyesore to read.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing characters read from text file in 2 different string
    By rcplguy15 in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2010, 04:25 AM
  2. Replies: 2
    Last Post: 02-06-2010, 11:45 AM
  3. Replies: 1
    Last Post: 02-05-2010, 02:59 PM
  4. Replies: 15
    Last Post: 11-12-2008, 06:12 PM
  5. How to read single characters in a string
    By MaxxMan-X in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 02:52 PM

Tags for this Thread