I need to find the position of some strings. These strings are stored in a file named queryfile , from an other file named datafile.
However, my program does not work as expected.
Can some one help me?
My program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *queryfile;
queryfile = fopen("op2query.txt","r");
FILE *datafile;
datafile = fopen("op2data.txt","r" );
int i = 1;
char word[99];
char search[99];
if(queryfile==NULL) {
printf("Error in reading Query File");
exit(1);
}
if(datafile==NULL) {
printf("Error in reading Data File");
exit(1);
}
while(fscanf(queryfile,"%98s",search)==1){
while(fscanf(datafile,"%98s",word)==1){
if (strcmp(word,search)==0){
printf("\n %i %s ", i, search);
rewind(datafile);
i=1;
break;
}
else
i++;
}
}
fclose(datafile);
fclose(queryfile);
return 0;
}
I build an array of each set of words to be tested, by splitting the query string into words. These words can span a line break in the data file. I mark the data file position on the second word of the set, if the search fails I seek to that point (if necessary). The program succeeds even if I duplicate every word "age" in the data file.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXWORDS 5
#define MAXLEN 99
int main()
{
int j, i, done, words, count;
long mark;
char word[MAXLEN];
char search[MAXLEN];
char *tok, *sptr[MAXWORDS];
FILE *queryfile;
FILE *datafile;
if ((queryfile = fopen("op2query.txt","r")) == NULL) {
printf("Error in reading Query File");
exit(1);
}
if ((datafile = fopen("op2data.txt","r" )) == NULL) {
printf("Error in reading Data File");
exit(1);
}
while(fgets(search, MAXLEN, queryfile) != NULL){
words = 0;
done = 0;
count = 0;
mark = -1;
tok = strtok(search, " \r\n");
while (tok && words < MAXWORDS) { // build array of query
sptr[words++] = tok;
tok = strtok(NULL, " \r\n"); // strips newline too
}
if (words < 1) // no more queries
break;
rewind(datafile); // beginning of file
while (!done) { // until none to read
count++;
if (mark >= 0) // when more than one word to search
fseek (datafile, mark, SEEK_SET);
mark = -1;
for (j=0; j<words; j++) {
if (j == 1) // mark for next search
mark = ftell(datafile);
if (fscanf(datafile, "%98s", word) != 1){
done = 1; // end of file
break;
}
if (strcmp(sptr[j], word)!=0)
break; // failed multi word search
}
if (done)
printf("NOT FOUND!");
else if (j == words) { // if all words found
printf("%d", count);
done = 1; // success
}
}
for (i=0; i<words; i++)
printf(" %s", sptr[i]); // show array of words asked
printf("\n");
}
fclose(datafile);
fclose(queryfile);
return 0;
}
Program output:
18 wisdom
40 season
NOT FOUND! summer
22 age of foolishness
UPDATE - I print NOT FOUND! when query not found. Added "summer" to query file.
you should put some debug-output behind the fscanf-calls (like printf("search:<%s> word:<%s>", search, word);
Then you will see, that fscanf stops at finding a white-space. you compare wisdom to each consecutive word in op2data.txt.
You should read line by line with fgets() removing the CR/LF from search.
But be aware, that the multi-word-search-word in data-file may be split between lines. like:
find me
i am the text to find
me in this file
so a better solution would be:
read search word by line (remove CR/LF) (normalize it by removing double spaces and not-letters)
read a chunk from datafile and normalize it too.
compare or continue by moving the read-position in data left by length of length of search word
Related
The text file has 225000 words. In the following sequence:
d
d
da
dog
dover
Denmark
lovethislifetoenjoy
beautifully
travelllerwise
lovethislifetoenjoy
alaskabluewheals
oceanisbluee
lovethislifetoenjoyfuntravel
basketball
lovethislifetoenjoyfuntravel
fashion
londonis
..
So the program should print the followings:
lovethislifetoenjoyfuntravel
travelllerwise
alaskabluewheals
oceanisbluee
lovethislifetoenjoyfuntravel
So far I coded this
int main()
{
FILE *fp;
errno_t err;
char current_Word[1024];
char longest_word[1024];
int ct=0;
char longWordList = malloc(sizeof(char*) * 10);
if ((err = fopen_s(&fp, "dictionary.txt", "r")) != 0)
{
printf("Cannot open file.\n");
return -1;
}
memset(longest_word, '\0', sizeof(longest_word));
while(fscanf_s(fp, "%s", current_Word, _countof(current_Word))!= EOF)
{
if (ct == 0)
{
strcpy(longest_word, current_Word);
}
if (strlen(current_Word) > strlen(longest_word))
{
strcpy(longest_word, current_Word);
}
ct++;
}
/* print */
printf("\nThe longest word is\n");
fprintf(stdout, "%s\n", longest_word);
fclose(fp);
return 0;
}
The program only print this -> lovethislifetoenjoyfuntravel
How I do keep track of all the longest words in a storage and then compare those words from the storage area with the word just read from input file?
If multiple words tied with the 5th longest word then end the program and print the final storage.
How to end the program when I find few tied with the 5th word that is the storage?
I do write code in Java and Python but this is something I need to completed for my department initiative.
I am using Visual Studio 2017 community edition for this project.
Your assistance will be greatly appreciated.
Thanks
The following should display the five longest words.
In addition it will also print additional words, which their sizes are the same as the shortest word of the five longest words in the file:
#define WORD_MAX_SIZE (50)
#define LONGEST_WORDS_COUNT (5)
int main()
{
char sCurWord[WORD_MAX_SIZE+1];
char arrLongestWords[LONGEST_WORDS_COUNT][WORD_MAX_SIZE+1];
int iShortestWordIndex = 0;
FILE* fp;
memset( arrLongestWords, 0, sizeof(arrLongestWords));
if( 0 != fopen_s(&fp, "C:\\Temp\\BlaBla.txt", "r"))
{
printf("Cannot open file.\n");
return -1;
}
while( fscanf_s( fp, "%1023s ", sCurWord, WORD_MAX_SIZE) == 1)
{
if( strlen(arrLongestWords[iShortestWordIndex]) < strlen(sCurWord))
strcpy_s( arrLongestWords[iShortestWordIndex], WORD_MAX_SIZE, sCurWord);
// Re-calculate shortest word index:
iShortestWordIndex = 0;
for( int i=1; i<LONGEST_WORDS_COUNT; i++)
{
if( strlen( arrLongestWords[i]) < strlen( arrLongestWords[iShortestWordIndex]))
{
iShortestWordIndex = i;
}
}
}
/* print */
printf("\n%d longest words are:\n", LONGEST_WORDS_COUNT);
for( int i=1; i<LONGEST_WORDS_COUNT; i++)
{
::printf( "\t%s\r\n", arrLongestWords[i]);
}
::printf( "\r\nOther words in the file, which their length are the same as the %dth largest word:\r\n", LONGEST_WORDS_COUNT);
// Move file-pointer back, to the beginning of the file:
rewind(fp);
// Now go over file once more to find all words, which their sizes are the
// same as the shortest word:
while( fscanf_s( fp, "%1023s ", sCurWord, WORD_MAX_SIZE) == 1)
{
if( strlen(arrLongestWords[iShortestWordIndex]) == strlen(sCurWord))
::printf( "\t%s\r\n", sCurWord);
}
fclose(fp);
return 0;
}
No real need for malloc, that is badly defined here.
you should keep a multi-dimensional array for the longest words like this:
char longest_word[5][1024];
also add a variable shortest_long_word_len:
int shortest_long_word_len = 0;
int shortest_long_word_location = 0;
Then as you test for current_Word, check its len vs. shortest_long_word_len. If longer, then strcpy the new word to the array in shortest_long_word_location
strcpy(longest_word[shortest_long_word_location], current_Word);
After that just recalculate shortest_long_word_len and shortest_long_word_location
I have a two filepaths input by the user and stored in an array. However, when I try to use one of these filepaths to open a file using fopen the code exits as if the file does not exist. If I hard code the filepath into the fopen function eveything proceeds perfectly.
For Example:
//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
//Global Functions
//Main Function
int main()
{
//Local Variables
char * user_input_full = NULL;
char user_input = 'a';
size_t len = 0; //Pointer for user_input_full
int size = 0; //Length of the input_use_full array
char *input_array[2]; //This hold the filepaths
int i=0; //A loop counter
//Carve out an initial array for the use input string (it is of unknown length)
user_input_full = calloc(16,sizeof(char));
size = 16;
//Take user input until a newline is encountered.
while(user_input != '\n')
{
scanf("%c", &user_input);
user_input_full[len] = user_input;
len = len+1;
if(len==size)
{
#ifdef DEBUG_MODE
printf("The input stream is being reallocated\n");
#endif
user_input_full = realloc(user_input_full,sizeof(char)*(size+16));
if(user_input_full == NULL)
{
//realloc failed, return a fail status
printf("Realloc of the input stream failed.\n");
return EXIT_FAILURE;
}
}
}
user_input_full[len] = '\0';
#ifdef DEBUG_MODE
printf("The user input was: %s \n", user_input_full);
#endif
//Parse out the file streams
input_array[i] = strtok(user_input_full," ");
while(input_array[i]!=NULL)
{
input_array[++i] = strtok(NULL," ");
}
#ifdef DEBUG_MODE
printf("Input array Values:\n");
for (i=0;i<2;i++)
{
printf("%s \n",input_array[i]);
}
#endif
//open the file specified in input_array[0]
if(fopen(input_array[0],"r") == NULL)
{
printf("Open of the input file failed.\n");
printf("Tried to open: %s ", input_array[0]);
return EXIT_FAILURE;
}
else
{
printf("The file opened.\n");
}
return EXIT_SUCCESS;
}
Would print the error block. However:
//Open the file specified in array[0]
if(fopen("test.txt","r") == NULL)
{
printf("Failed to open the input file.\n");
return EXIT_FAILURE;
}
else
{
printf("The file opened.\n");
}
Works perfectly fine.
I also tried passing the value in input_array[0] to a const char*, but that did not work either. I feel like I am missing some fundamental concept here.
EDIT: Clarified one of the comments in the code block
The real reason it is failing is due to trailing newline in the filename. All you need is to add newline as string token.
Change this line:
input_array[i] = strtok(user_input_full," ");
to:
input_array[i] = strtok(user_input_full," \n");
and this line:
input_array[++i] = strtok(NULL," ");
to:
input_array[++i] = strtok(NULL," \n");
This will remove the trailing newlines.
First mistake I note is in the following code block. it's inputing 3 three strings to input_array and going out of bound.
//Parse out the file streams
input_array[i] = strtok(user_input_full," ");
while(input_array[i]!=NULL)
{
input_array[++i] = strtok(NULL," ");
}
Change this code block to something as shown here.
input_array[i] = strtok(user_input_full," ");
for(i=1;i<2;i++)
{
input_array[i] = strtok(NULL," ");
}
I have created a function that takes as a parameter the name of a source file, the name of a destination file and the beginning and end lines of the source file lines that will be copied to the destination file, like the example below. All I want to do is to input the lines that I want to copy to the other text file like the example below:
The code I show you just "reads" the content of the one text file and "writes" another one. I want to "write" specific lines that the user gives, not the whole text file
Inputs by the user:
Source_file.txt //the file that the destination file will read from
destination_file.txt //the new file that the program has written
2 3 // the lines that it will print to the destination file: 2-3
Source_file.txt:
1
2
3
4
5
6
destination_file.txt
2
3
code:
#include <stdio.h>
#include <stdlib.h>
void cp(char source_file[], char destination_file[], int lines_copy) {
char ch;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("File name not found, make sure the source file exists and is ending at .txt\n");
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, destination);
printf("Copied lines %d from %s to %s \n",
lines_copy, source_file, destination_file, ".txt");
fclose(source);
fclose(destination);
}
int main() {
char s[20];
char d[20];
int lines;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the number of lines you want to copy\n\n");
printf(">subcopy.o ");
gets(s);
printf("destination file-> ");
gets(d);
printf("Lines: ");
scanf("%d", &lines);
cp(s, d, lines);
return 0;
}
In cp(), in order to select the lines to keep, you have to know their position in the input-file. Thus, you need to count lines.
Using fgets instead of fgetc will allow you to count the lines.
On the other hand, if I wanted to select lines 3 and 7 to 12 in a file, I'd use:
sed -n -e "3p;7,12p" < input.txt > output.txt
this is a very simple solution, let's say you know that the maximun length of a line will be 100 characters for simplicity (if a line is longer than 100 characters only the first 100 will be taken)
at the top (outside main) you can write
#ifndef MAX_LINE_SIZE
#define MAX_LINE_SIZE 100
#endif
i know many people don't like this but i think in this case it makes the code more elegant and easier to change if you need to modify the maximum line size.
to print only the wanted lines you can do something like this
char line[MAX_LINE_SIZE];
int count = 0;
while (fgets(line, MAX_LINE_SIZE, source)){
count++;
if (3 <= count && count <= 5){
fputs(line, destination);
}
}
The while loop will end when EOF is reched because fgets returns NULL.
P.S. there could be some slight errors here and there since i wrote it pretty fast and going by memory but in general it should work.
There are some problems in your program:
Do not use gets(), it may cause buffer overflows.
Always use type int to store the return value of fgetc() in order to distinguish EOF from regular byte values.
You pass an extra argument ".txt" to printf(). It will be ignored but should be removed nonetheless.
To copy a range of lines from source to destination, you can just modify your function this way:
#include <stdio.h>
#include <string.h>
#include <errno.h>
void cp(char source_file[], char destination_file[], int start_line, int end_line) {
int ch;
int line = 1, lines_copied;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("Cannot open input file %s: %s\n",
source_file, strerror(errno));
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
printf("Cannot open output file %s: %s\n",
destination_file, strerror(errno));
fclose(source);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF) {
if (line >= start_line && line <= end_line) {
fputc(ch, destination);
}
if (ch == '\n') {
line++;
}
}
lines_copied = 0;
if (line > start_line) {
if (line >= end_line) {
lines_copied = end_line - start_line + 1;
} else {
lines_copied = line - start_line + 1;
}
}
printf("Copied lines %d from %s to %s\n",
lines_copy, source_file, destination_file);
fclose(source);
fclose(destination);
}
int main() {
char source_file[80];
char destination_file[80];
int start_line, end_line;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the start and end line\n\n");
printf(">subcopy.o ");
if (scanf("%79s", source_file) != 1) {
return 1;
}
printf("destination file-> ");
if (scanf("%79s", destination_file) != 1) {
return 1;
}
printf("Start and end lines: ");
if (scanf("%d %d", &start_line, &end_line) != 2) {
return 1;
}
cp(source_file, destination_file, start_line, end_line);
return 0;
}
My task is to find word palindromes in a text file and to NOT print them into results file. The results file should only contain all the spaces and words that are NOT palindromes. I've been working on this program for two solid weeks, but as I am a total newb in C, I can't simply imagine how to do this correctly. Also, I have to work in Linux environent, so I can't use commands like strrev() which would make my life a lot easier at this point...
Anyways, data file contains a lot of words in a lot of lines separated by quite a few spaces.
Here is the program that is working, but doesn't work with any spaces, because I don't know how to check them at the needed place.
#include <stdio.h>
#include <string.h>
const int CMAX = 1000;
const int Dydis = 256;
FILE *dataFile;
FILE *resFile;
void palindrome(char *linex);
int main(){
char duom[CMAX], res[CMAX], linex[Dydis];
printf("What's the name of data file? \n");
scanf("%s", duom);
dataFile=fopen(duom, "r");
if (dataFile==NULL){
printf ("Error opening data file \n");
return 0;
};
printf("What's the name of results file? \n");
scanf ("%s", res);
resFile=fopen(res, "w");
if (resFile==NULL){
printf ("Error opening results file \n");
return 0;
};
while (fgets(linex, sizeof(linex), dataFile)) {
palindrome(linex);
}
printf ("all done!");
fclose(dataFile);
fclose(resFile);
}
void palindrome(char *linex){
int i, wordlenght, j;
j = 0;
char *wordie;
const char space[2] = " ";
wordie = strtok(linex, space);
while ( wordie != NULL ) {
wordlenght = strlen(wordie);
if (wordie[j] == wordie[wordlenght-1]) {
for (i = 0; i < strlen(wordie); i++) {
if (wordie[i] == wordie[wordlenght-1]) {
if (i == strlen(wordie)-1) {
fprintf(resFile,"");
}
wordlenght--;
}
else {
fprintf(resFile,"%s", wordie);
break;
}
}
}
else {
fprintf(resFile,"%s", wordie);
}
wordie = strtok(NULL, space);
}
}
EDIT:
Code below works as following:
input file is read char by char
if char read isn't alphanumeric, then it is written to the output file
else, the whole word is read with fscanf
if word is not a palindrome, then write to the output file
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int is_pal(char* word) {
size_t len = strlen(word);
char* begin = word;
char* end = word + len - 1;
if (len == 1) {
return 1;
}
while (begin <= end) {
if (*begin != *end) {
return 0;
}
begin++;
end--;
}
return 1;
}
int main(void)
{
FILE* fin = fopen("pals.txt", "r");
if (fin == NULL) {
perror("fopen");
exit(1);
}
FILE* fout = fopen("out_pals.txt", "w");
if (fout == NULL) {
perror("fopen");
exit(1);
}
int ret;
char word[100];
while ((ret = fgetc(fin)) != EOF) {
if (!isalpha(ret)) {
fprintf(fout, "%c", ret);
}
else {
ungetc(ret, fin);
fscanf(fin, "%s", word);
if (!is_pal(word)) {
fprintf(fout, "%s", word);
}
}
}
fclose(fin);
fclose(fout);
return 0;
}
I've created file with following content:
cancer kajak anna sam truck
test1 abc abdcgf groove void
xyz annabelle ponton belowoleb thing
cooc ringnir
The output file :
cancer sam truck
test1 abc abdcgf groove void
xyz annabelle ponton thing
(line with two spaces)
As you can see, the number of spaces between words are the same as in the input file.
I've assumed that single word could have 100 chars maximum. If there would be longer words, reading with fscanf onto fixed-size buffer can be harmful.
Hints:
strtok() gives you a pointer to the start of delimited words but it does not
extract them or put them in their own string for you.
You need some logic to find the end of each word. The function
strlen() will tell you how many characters there are from the char*
that it gets until a null-character. If you give it a pointer to the start
of a word within a sentence it will give you the length from the start of the
word to the end of the sentence.
Breaking palindrome() into a function that loops over words in a line and a
function that returns whether or not a single word is a palindrome
may help.
Your for loop is checking each pair of letters twice. i only needs to scan over half
of the word length.
You only need a single if within palindrome(). I'm not sure why you have so many.
They're redundant.
I have a hashtable ADT which has two functions, insert and lookup. I put in to the insert function a hash table, hash table size, ID #, and book title and that inserts it into the hash table. This works fine when I pass it a string literal, i.e. insert(...,"Hello, world!"...); It doesn't work when I read in strings from a file, store them in an array, and try and use my insert and lookup functions.
I have all of my code here but the most important files are main.c and hash.c. Hash.c has the newHash(), hash(), insert(), and lookup() functions and main.c reads from two files, in this case test1.lib.in and test1.req.in, and from the first file will get the library id and title of a book from each line and then put it in the hash table. From the second file, it gets requests for a book title and should print the ids in its linked list.
List of links to files https://docs.google.com/document/d/1tFNs-eVkfnCfjwAHcAUdHtUl1KVv_WcnW2IS0SRFvcM/edit?usp=sharing
Example of code that works.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list.h"
#include "hash.h"
int main(){
ListHndl* temp = newHash(10);
insert(442440, "cvyaqbznxel", 10,temp);
lookup(temp,"cvyaqbznxel", 10);
return 0;
}
Code that doesn't work
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list.h"
#include "hash.h"
int main(int argc, char * argv[]) {
if (argc != 3) {
printf("Incorrect arguments, please specify 2 files to be read\n");
return EXIT_FAILURE;
}
FILE *file = fopen( argv[1], "r");
FILE *secondFile = fopen(argv[2], "r");
if (file == 0 || secondFile == 0) {
printf("Could not open a file\n");
return EXIT_FAILURE;
}
int numDataLines2;
int numDataLines;
int hashTableSize;
//First line of first file gives number of lines in file and
//size of hash table to be made
if(fscanf(file, "%d%d", &numDataLines, &hashTableSize) < 2) {
printf("Unable to parse first line of first file\n");
return EXIT_FAILURE;
}
ListHndl* theHash = newHash(hashTableSize);
int libraryID;
char *tempString = calloc(numDataLines,41*sizeof(char));
char lineHolder[129];
//discard the new line which always shows up
fgets(lineHolder, 128, file);
for(int i = 0; i < numDataLines; i++) {
//Gets the whole line to be scanned with sscanf
fgets(lineHolder, 128, file);
//If the line consists of just a newline char, continue
if(strcmp(lineHolder, "\n") == 0 ) {
continue;
}
//Scans the line retrieved from fgets and placed in lineHolder
if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID,&tempString[i]) == 0){
printf("Unable to parse line %d of first file\n",i+2);
return EXIT_FAILURE;
}
insert(libraryID, &tempString[i], hashTableSize, theHash);
}
char String[41];
fgets(String, 40, secondFile);
numDataLines2 = atoi(String);
char *storeSecondFileStuff = calloc(numDataLines2,41*sizeof(char));
for(int i = 0; i< numDataLines2; i++) {
fgets(lineHolder, 128, secondFile);
if(strcmp(lineHolder, "\n") == 0) {
continue;
}
if(sscanf(lineHolder, "%40[^\n]",&storeSecondFileStuff[i]) == 0) {
printf("Unable to parse line %d of second file\n",i+2);
return EXIT_FAILURE;
}
lookup(theHash, &storeSecondFileStuff[i], hashTableSize);
}
printf("\n");
fclose(file);
fclose(secondFile);
return 0;
}
Thanks!
I think you have multiple problems. To start with, you might not be scanning your input line correctly. Change your line
if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID,&tempString[i]) == 0)
to
if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID, tempString) < 0)
that way, you will trap the situation where the sscanf function did not successfully convert both arguments - for example, if there is no comma in the input line. Note that sscanf returns the number of successful conversions; success would return a value of 2, so testing for <2 is the right way to go.
Note also that I changed &tempString[i] to tempString. The former points to some place along tempString - which only has 41 characters allocated to it. Yet you always allow up to 40 characters (plus '\0' to be written to it - so you will write past the end of the string. Since this is only a temporary variable, there is no sense in doing this. Just scan the input into the temp variable, then do whatever you need to do with it.
This means that your insert also changes, from
insert(libraryID, &tempString[i], hashTableSize, theHash);
to
insert(libraryID, tempString, hashTableSize, theHash);
Again, you need to do the same thing lower down in your code.
Here is an attempt at making the code work for you - see if this hits the spot. Note that all I really did was change the type of tempString and storeSecondFileStuff, and modified the way they were used in various function calls accordingly. I did not attempt to compile / run because of the complexity of the other files involved - but this should help a bit:
int main(int argc, char * argv[]) {
if (argc != 3) {
printf("Incorrect arguments, please specify 2 files to be read\n");
return EXIT_FAILURE;
}
FILE *file = fopen( argv[1], "r");
FILE *secondFile = fopen(argv[2], "r");
if (file == 0 || secondFile == 0) {
printf("Could not open a file\n");
return EXIT_FAILURE;
}
int numDataLines2;
int numDataLines;
int hashTableSize;
//First line of first file gives number of lines in file and
//size of hash table to be made
if(fscanf(file, "%d%d", &numDataLines, &hashTableSize) < 2) {
printf("Unable to parse first line of first file\n");
return EXIT_FAILURE;
}
ListHndl* theHash = newHash(hashTableSize);
int libraryID;
char **tempString = calloc(numDataLines,sizeof(char*)); // <<< ARRAY of pointers
char lineHolder[129];
//discard the new line which always shows up
fgets(lineHolder, 128, file);
for(int i = 0; i < numDataLines; i++) {
//Gets the whole line to be scanned with sscanf
fgets(lineHolder, 128, file);
tempString[i] = calloc(1, 41 * sizeof(char)); // <<< space for this string
//If the line consists of just a newline char, continue
if(strcmp(lineHolder, "\n") == 0 ) {
continue;
}
//Scans the line retrieved from fgets and placed in lineHolder
if(sscanf(lineHolder, "%d, %40[^\n]", &libraryID, tempString[i]) < 0){ // <<< changed
printf("Unable to parse line %d of first file\n",i+2);
return EXIT_FAILURE;
}
insert(libraryID, tempString[i], hashTableSize, theHash); // <<< changed
}
char String[41];
fgets(String, 40, secondFile);
numDataLines2 = atoi(String);
char **storeSecondFileStuff = calloc(numDataLines2, sizeof(char*)); // changed: again char **
for(int i = 0; i< numDataLines2; i++) {
fgets(lineHolder, 128, secondFile);
storeSecondFileStuff[i] = calloc(1, 41 * sizeof(char));
if(strcmp(lineHolder, "\n") == 0) {
continue;
}
if(sscanf(lineHolder, "%40[^\n]",storeSecondFileStuff[i]) == 0) {
printf("Unable to parse line %d of second file\n",i+2);
return EXIT_FAILURE;
}
lookup(theHash, storeSecondFileStuff[i], hashTableSize); // <<<< changed
}
printf("\n");
fclose(file);
fclose(secondFile);
return 0;
}