Hey all!
If I have the below code snippets:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define FILELINELENGTH 1000
void insertstring(struct data *data, int counter2);
FILE *p_openfile;
int main(void)
{
.
.
.
.
// Extra code here //
.
.
.
if((p_line = malloc(FILELINELENGTH * sizeof(char))) == NULL )
{
perror("Error allocating memory");
exit (EXIT_FAILURE);
}
for( counter = 0; counter < lastfilenumber1; counter++)
{
int found = 0;
if((p_openfile = fopen(LineData[counter].storefilename, "r")) == NULL)
{
printf("error opening file: %s", LineData[counter].storefilename);
exit(EXIT_FAILURE);
}
while((fgets(p_line, FILELINELENGTH, p_openfile)) != NULL)
{
if((p_searchstring = strstr(p_line, p_searchtext) != NULL))
{
strcpy(LineData[counter1].filename1, LineData[counter].storefilename);
found = 1;
counter1++;
break;
}
}
if(!found) // if search srting(p_searchtext) not found
{
strcpy(LineData[counter2].filename2, LineData[counter].storefilename);
insertstring(LineData, counter2);
counter2++;
}
fclose(p_openfile);
}
free(p_line);
return 0;
}
void insertstring(struct data *data, int counter2)
{
char *p_line1;
printf("\n%s", LineData[counter2].filename2);
if((p_line1 = malloc(FILELINELENGTH * sizeof(char))) == NULL )
{
perror("Error allocating memory");
exit (EXIT_FAILURE);
}
while((fgets(p_line1, FILELINELENGTH, p_openfile)) != NULL)
{
printf("\n%s", p_line1);
}
}
All the code in the main() works fine.
What I want to do is to print out every line of the files that don't contain the search string(p_searchtext).
Why does the last line of code(in red) not print? I've used the debugger, and the debugger just skips the while loop to the closing brace. At this point, p_line1 contains garbage, instead of a line of text.
I can post the complete code, if necessary.
Thanks in advance.