Reputation: 79
I want to write a program to print all words containing a letter ("D" for example) in a text file.
This is what I came up with, but it does not work.
I get the core dumped error.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
int findWord(FILE *file , const char *letter);
void main()
{
FILE *file;
char path[100];
char letter[1];
printf("Enter file path: ");
scanf("%s", path);
printf("Enter letter to search in file: ");
scanf("%s", letter);
file = fopen(path, 'r');
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
int findWord(FILE *file, const char *letter)
{
char str[BUFFER_SIZE];
while ((fgets(str, BUFFER_SIZE, file)) != NULL)
{
if (str == letter)
{
printf(letter);
}
}
}
Upvotes: 0
Views: 942
Reputation: 2520
Edited your code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
int findWord(FILE *file , const char letter);
int main()
{
FILE *file;
char path[100];
char letter;
// const char *fpath = "/home/kozmotronik/Belgeler/karalama.txt";
printf("Enter file path: ");
// fgets(path, sizeof(path), stdin);
// printf("Entered file path: %s\n", path);
scanf("%s", path);
// flushes the standard input
// (clears the input buffer)
// If we don't do this we cannot get the letter from the input buffer
while ((getchar()) != '\n');
printf("Enter letter to search in file: ");
letter = getchar();
// Validity check
if(letter < '0' || letter > 'z') {
printf("Entered character is not valid\n");
exit(EXIT_FAILURE);
}
file = fopen(path, "r");
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
int findWord(FILE *file, const char letter)
{
char str[BUFFER_SIZE];
while ((fgets(str, BUFFER_SIZE, file)) != NULL)
{
printf("Looking for '%c' in %s\n", letter, str);
char *c = strchr(str, (int)letter);
if (c != NULL)
{
printf("'%c' found in %s\n", *c, str);
}
}
return 0;
}
In the same directory of the source code, created afile.txt file and entered the file name. Here is the output for the program:
Enter file path: afile.txt
Enter letter to search in file: e
Looking for 'e' in This file contains some text.
'e' found in This file contains some text.
Looking for 'e' in This file is used for testing purposes.
'e' found in This file is used for testing purposes.
Looking for 'e' in This file must be read only.
'e' found in This file must be read only.
And here is the afile.txt file content:
This file contains some text.
This file is used for testing purposes.
This file must be read only.
You are encouraged to split a line of string into the words and search the letter in the word list.
Upvotes: 2
Reputation: 81
#include <stdio.h>
#include <stdlib.h>
#include <mem.h>
#define BUFFER_SIZE 1000
void findWord(FILE *file , const char *letter);
int main()
{
FILE *file;
char path[100];
char letter[2];
printf("Enter file path: ");
scanf("%s", path);
printf("Enter letter to search in file:");
scanf("%s", letter);
file = fopen(path, "r");
if (file == NULL)
{
printf("Unable to open file.\n");
printf("Please check you have read/write priveleges.\n");
exit(EXIT_FAILURE);
}
findWord(file, letter);
fclose(file);
return 0;
}
void findWord(FILE *file, const char *letter)
{
char str[BUFFER_SIZE];
do
{
fscanf(file, "%s", str);
if(strchr(str, letter[0]) > 0){
printf("%s\n", str);
}
}while (fgetc(file) != EOF);
}
Upvotes: 1