Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_LEN 200

int main(){
    FILE * fp1 = fopen("file.csv", "r");
    char string[STRING_LEN], pinFind[STRING_LEN];
    char * pinFound = NULL;
    
    printf("Enter the pin: ");
    scanf("%s", pinFind);

    while(fgets(string, STRING_LEN, fp1)){
      pinFound = strstr(string, pinFind);
      if(pinFound){
        char * word1 = strtok(string, ",");
        char * word2 = strtok(NULL, ",");
        char * word3 = strtok(NULL, ",");
        char * word4 = strtok(NULL, ",");
        char * word5 = strtok(NULL, ",");
        char * word6 = strtok(NULL, ",");
        
        printf("%s", word6);
      }
    }
    return 0;
}


What I have tried:

In this code, I am trying to take the last word from a file but don't know how to do it? The result is like this.
18-10-2020
19-10-2020
20-10-2020

If I use strrchr() to take the last occurence then the last the occurence is newline and it won't return and any result. But I use strchr then the first occurence is also newline, so it won't take the last occurence. Instead the second last occurence will be taken but I want to print the last one.

Here is the file data
Name,ID,Phone number,Pin code,Deposit,Date
Ali Ahmed1,2222222222222,77777,6677,500,17-10-2020
Bilal Khan,2222222222222,77777,1122,500,18-10-2020
Ali Ahmed1,2222222222222,77777,6677,500,17-10-2020
Talha,2222222222222,77777,1122,500,19-10-2020
Ali Ahmed1,2222222222222,77777,1122,500,20-10-2020
Bilal Khan,2222222222222,77777,6677,500,17-10-2020
Posted
Updated 19-Oct-20 20:16pm
v2

I showed you a function that will parse out as many tokens as you want and you are doing this in your code?

All right then.

Why do you have the same literal delimiter string noted six times? They are all the same so make ONE of them. If you don't want newlines then make it part of the one and only delimiter string.

Here's an example :
C++
#define MAX_TOKENS 16
    const char * delims = ",\n";
    char * tokens[ MAX_TOKENS ] = { NULL };
    char pinFind[ STRING_LEN ] = { 0 };   // must have storage for PIN
    int count = 0;
    int x;

    // acquire the user's PIN
    // open the file here

    // read a line of text from the file into string

    while( fgets( string, STRING_LEN, fp1 ) )
    {
        count = ParseTokens( string, delims, tokens, MAX_TOKENS );
        if( count < 6 )  // expect 6 tokens per line
            continue;    // if we don't get them skip this line

        // PIN is the fourth token 

        if( strcmp( pinFind, tokens[ 3 ] ) == 0 )
        {
            printf( "deposit on %s was for %s\n", tokens[ 5 ], tokens[ 4 ] );
            break;
        }
    }

    // close the file
This uses the ParseTokens function I gave you before because it is very easy. I use it often to parse a wide variety of different types of files.
 
Share this answer
 
v2
What's wrong with
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STRING_LEN 200

int main()
{
  FILE * fp = fopen("file.csv", "r");
  char string[STRING_LEN];

  while(fgets(string, STRING_LEN, fp))
  {
    char * plast = NULL, * pcur = string;
    while ( (pcur = strtok( pcur, "," )))
    {
      plast = pcur;
      pcur = NULL;
    }
    printf("%s\n", plast);
  }
  return 0;
}
?
 
Share this answer
 
Comments
ibilalkayy 20-Oct-20 3:40am    
I just want to show the user that his last amount was deposited on this date.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900