In: Computer Science
In C programming,
I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 0x3000
char buf[BUF_SIZE];
int main()
{
char* inputFile = "DOISigners.txt";
FILE* fp;
fp = fopen(inputFile, "r");
if (!fp)
{
char errorMsg[256] = "Could not
open error file ";
strcat(errorMsg, inputFile);
perror(errorMsg);
getchar();
exit(-1);
}
int size = fread(buf, sizeof(char), sizeof(buf),
fp);
buf[size] = '\0';
fclose(fp);
#define NUM_NAMES 3
char* namesToSearchFor[NUM_NAMES] = { "John" };
char* stringToSearch = "J";
int length = strcspn(stringToSearch, ",");
for (int i = 0; i < NUM_NAMES; i++)
{
char* start = strstr(buf,
namesToSearchFor[i]);
if (start != NULL)
{
printf("%s\n\n",
namesToSearchFor[i]);
}
}
getchar();
exit(0);
}
and here is from the .txt file
Georgia: Button Gwinnett, Lyman Hall, George Walton
North Carolina: William Hooper, Joseph Hewes, John Penn
South Carolina: Edward Rutledge, Thomas Heyward, Jr., Thomas Lynch, Jr., Arthur Middleton
Massachusetts: John Hancock
Maryland: Samuel Chase, William Paca, Thomas Stone, Charles Carroll of Carrollton
Virginia: George Wythe, Richard Henry Lee, Thomas Jefferson, Benjamin Harrison, Thomas Nelson, Jr., Francis Lightfoot Lee, Carter Braxton
Pennsylvania: Robert Morris, Benjamin Rush, Benjamin Franklin, John Morton, George Clymer, James Smith, George Taylor, James Wilson, George Ross
Delaware: Caesar Rodney, George Read, Thomas McKean
New York: William Floyd, Philip Livingston, Francis Lewis, Lewis Morris
New Jersey: Richard Stockton, John Witherspoon, Francis Hopkinson, John Hart, Abraham Clark
New Hampshire: Josiah Bartlett, William Whipple
Massachusetts: Samuel Adams, John Adams, Robert Treat Paine, Elbridge Gerry
Rhode Island: Stephen Hopkins, William Ellery
Connecticut: Roger Sherman, Samuel Huntington, William Williams, Oliver Wolcott
New Hampshire: Matthew Thornton
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 0x3000
char buf[BUF_SIZE];
int main()
{
char* inputFile = "DOISigners.txt";
FILE* fp;
fp = fopen(inputFile, "r");
if (!fp)
{
char errorMsg[256] = "Could not open error file ";
strcat(errorMsg, inputFile);
perror(errorMsg);
getchar();
exit(-1);
}
int size = fread(buf, sizeof(char), sizeof(buf), fp);
buf[size] = '\0';
fclose(fp);
#define NUM_NAMES 3
/**** Crash Was here Array size was 3 but ,You have defined only
one string
* So we are accessing invalid index and crash/exit happened
*/
// Old code
//char* namesToSearchFor[NUM_NAMES] = { "John"};
//Define three names if you want to search all three
char* namesToSearchFor[NUM_NAMES] = { "John","Thomas","Treat"};
//Start
for (int i = 0; i < NUM_NAMES;)
{
//Start from file Buffer position 0
int wordLocation = 0;
printf("\n=== Searching word : %s =======
",namesToSearchFor[i]);
while(1)
{
printf("\n");
//Find Matching first Substring
char* start = strstr(buf+wordLocation, namesToSearchFor[i]);
if(start == NULL) break;
//find Position in buffer where Matching substring found
wordLocation += start - (buf+wordLocation);
//Read Position is different then wordLocation
//Example Word "Treat"
//in file for name " Robert treat Paine "
//wordLocation = 't'
//readPosition = 'R'
//Output = Robert treat Paine
int readPosition = wordLocation;
//If word is middle name than go to start of word
while(1)
{
//Go Backword at starting of word
//until EOF NewLine or , not found
char ch = buf[readPosition];
if(ch == EOF ||
ch == '\n' ||
ch == ':' ||
ch == ',')
{
//+2 to overcome space before each word
readPosition+=2;
break;
}
readPosition--;
}
int charPosition = 0;
while(1)
{
//Read and find each charator in order J h o n until EOF NewLine or
, not found
char ch = buf[readPosition+charPosition];
if(ch == EOF ||
ch == '\n' ||
ch == ',')
{
break;
}
charPosition++;
printf("%c",ch);
}
//Move to next occurance of that word in file
wordLocation+=charPosition;
}
//Move to new word
i++;
}
printf("\n============ All search
Completed=============\n\t");
fflush(stdout);
getchar();
exit(0);
}
/**
Output
=== Searching word : John =======
John Penn
John Hancock
John Morton
John Witherspoon
John Hart
John Adams
=== Searching word : Thomas =======
Thomas Heyward
Thomas Lynch
Thomas Stone
Thomas Jefferson
Thomas Nelson
Thomas McKean
=== Searching word : Treat =======
Robert Treat Paine
============ All search Completed=============
**/