Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,495 people online right now. Registration is fast and FREE... Join Now!




sudden segmentation fault error

 
Reply to this topicStart new topic

sudden segmentation fault error

Tensaijin
2 Dec, 2006 - 07:58 PM
Post #1

New D.I.C Head
*

Joined: 2 Dec, 2006
Posts: 1


My Contributions
Hello,

I have written a program in c which searches for a string in a text file. The program works, however I get a 'segmentation fault' error after the program terminates. I believe I have tracked the problem down to the while loop inside function Read(). Any help is much appreciated.

Thanks,

--------------------------------------------------------------------------
CODE
#include <stdio.h>
#include <string.h>

/* Declare static maximum array size */
const int MAX_INPUT_SIZE = 60000;

/************************************************** *************
Task: Read text file and search for string
Implementation: Asks the user to input a string they would like
to search for, and loads each string array 'index' with a line
from the file using strstr(). The program searches for the user's
string as each line is read. The program then notifies the user
of the results.
************************************************** **************/
void Read(FILE* file)
{
char* _string;

char token[MAX_INPUT_SIZE];

int count = 0;

printf("\nString to search for: ");

fflush(stdout);

fgets(token, 10, stdin);

if (token[strlen(token) - 1] == '\n')
{
token[strlen(token) - 1] = '\0';

} // End if

while (fgets(_string, 1024, file) != NULL)
{
if (strstr(_string, token) != NULL)

count++;

_string = _string + 1;

} /* End while */

if (count != 0)

printf("\nSuccess!!!\n\nString found: %d times\n\n", count);

else

printf("\nString not found\n\n");

} /* End read */

/************************************************** *************
Task: Program entry point main()
Implementation: main asks the user to input a file for reading.
Afterwards it calls function Read() to search for a string.
************************************************** **************/
int main(int argc, char *argv[])
{
FILE * doc;

char filename[50];

printf("\nFile to open: ");

fgets(filename, 50000, stdin);

/* Ensures the file name has no return characters */
if (filename[strlen(filename) - 1] == '\n')
{
filename[strlen(filename) - 1] = '\0';

} // End if

/* Open file */
doc = fopen(filename, "r");

if (!doc)
{
printf("\nFile not found!\n");

exit(1);

} // End if

/* With file opened, call function to read file */
Read(doc);

fclose(doc);

return 0;

} /* end main */

User is offlineProfile CardPM
+Quote Post

msg555
RE: Sudden Segmentation Fault Error
2 Dec, 2006 - 08:03 PM
Post #2

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 2 times
Dream Kudos: 25
My Contributions
I don't see where you are initializing _string. It appears to me like you are loading part of the file into a random pointer.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month