Look at this conditional:
Code:
if(!strstr(haystack, needle) == NULL)
{
inputFile.close();
cout << "finished";
break;
Suppose "</script>" is found in the file. That means the comparison:
strstr(haystack, needle) == NULL
will be false. Then, !false evalutes to true, so the if block is executed, and in the if block, you close the file and output "finished". I don't think that is what you want to do when you find "</script>" in the file.
When you get that straightened out, you still aren't quite grasping the complexity of what you have to do. This is what your program does now:
Code:
haystack = new char [bufferlength];
...
inputFile.getline(haystack, bufferlength, '¥');
...
cout << haystack;
outputFile << haystack;
After using the getline() function, the variable haystack will contain your whole file, and then you write the whole file to outputFile. If you are intent on using char arrays, then this is what you have to do:
Quote:
2) strstr() will return a pointer to the first occurrence of "</script>", which means it will return a pointer to the '<' char.
But, you want a marker that points to the end of the </script> tag, so you need to move the pointer to the end of the tag:
Quote:
If you name that pointer 'end', then using pointer arithmetic you can add 8 to end and get a pointer to the '>' char.
That gives you a marker to the spot in the char array buffitUP, namely the closing '>' character of the </script> tag, that is the end of the data you want to extract.
Quote:
Then, once again using pointer arithmetic, you can get the difference between buffitUP and end, which will give you the number of char's in the char array from the start of the array to the '>' character in the </script> tag.
buffitUP is a pointer to the first char in the array, and after doing the above steps, the variable end will be a pointer to '>' character in the </script> tag. If you subtract those pointers, the difference is an integer that will be the total number of characters you want to copy from the char array buffitUP.
Quote:
Then, using a for loop and the difference as the loop conditional, you can copy each char from buffitUP to another char array that holds the result, and finally tack on a '\0' character after the last character. Whew!
That means you need to create a for-loop starting at 0 and ending once the specified number of characters has been copied. You also need to create a char array that is going to hold the char's you copy from buffitUP. Then, you just loop through buffitUP, which is an array, so you can use subscript notation to pick out the char's, and then you can assign them to the result array, e.g.:
buffitUP[35] = result[35];
Once you get the proper portion of buffitUP copied to result, and it displays properly using cout<<, you can write result to an output file.
I suggest you attempt the following before writing the whole program. If you can't do the following, you won't be able to do the steps outlined above. Here is a char array:
char data[] = "hello world";
1) Use strstr() to get a pointer to the 'w'. Then, subtract the pointer to 'h', which is data, from the pointer to 'w' to get the number of char's between the two pointers. To be clear, you want:
pointer to 'w' - pointer to 'h'
not the other way around. Then, use a for loop and that integer number to end the loop. Inside the for-loop copy the characters in data to another char array.
2) Next, write a program that uses the pointer to 'w' to get a pointer to the char 'd' in data. Then, get the difference between that pointer and a pointer to the first char(i.e. data) and use a for loop to copy the characters in data up to and including the 'd' into another array.
When you are trying to write a program with concepts you've never tried before, or don't quite understand, it is better to first attempt a smaller practice program that eliminates the file I/0, and focuses on doing just the core operations.