PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 2, 2022

[FIXED] How do I get specific names from a text file with a location as condition?

 November 02, 2022     c, file, string     No comments   

Issue

Please help me. I'm new to programming. I've been trying and searching for days to answer this.

I need a C program that will open a text file named users.txt

In that text file there are names then comma and their schools.

John Paul, Legit Univ
Paul John, Yo Univ
Lebron James, School Univ
James Lebron, Legit Univ

All I managed so far is to display them all. The output should be all the users that are from "Legit Univ".

Sample output:

Found 2 users from Legit Univ

John Paul
James Lebron

Solution

Use fgets() to read a line from file into a string, then strchr() to to find the position of the comma ',' field separator in the string (or strstr() if the field separator is comma space ", "). Now you can check the part of the string after the field separator for a match on your query with strcmp(). Instead of parsing the file, you could also use a regex and match against the string.



Answered By - Allan Wind
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing