Share and enjoy.
PHP Code:
#define _CRT_SECURE_NO_WARNINGS
//For all our file reading functions and printf()
#include <cstdio>
//For seacrhing through the strings
#include <cstring>
//For dynamic memory allocation.
#include <cstdlib>
//For calling system(). Be aware that system("pause") is a Windows command
//which causes the "Press any key to continue..." message to show up.
//On other systems (Linux, Macs, etc.), pause is not a valid command.
//Another way would be to print "Press enter to continue..." and then call getchar(),
//which waits for the user to hit enter.
#include <iostream>
int main(void)
{
//Strings that hold our start and end key phrases.
//I'm making them constant, but if you want to implement a way to change them, go ahead.
const char* startKey = "World!";
const char* endKey = "I am";
//Create our file pointer. I'm using this system instead of ifstream because
//I learned C before C++ so I've never gotten around to using the C++ file system.
//Feel free to use whichever you want. Both work identically.
FILE* file;
//Call fopen(), which takes two arguments: the file name and a mode. We're using "r"
//for "read" mode. "w" is for write, and you can look up other codes online by Googling fopen.
//Also, if you were reading or writing binary data instead of text, you'd use "wb" or "rb"
//Two backslashes are used after "C:" because \ is reserved for special characters, such as
// \n, which jumps to the next line. "\\" is the code for \.
//fopen() also won't work if the file doesn't exist yet. Make sure this file exists and is in the
//correct location.
file = fopen("C:\\TestText.txt", "r");
//if fopen() fails, it sets the file pointer to NULL. We'll check for that and quit the program
//if this happens.
if(file == NULL)
{
printf("File couldn't be opened properly.\n");
system("pause");
return 1;
}
//I'm going to get the length of the file by calling fseek() to go the end of the file,
//then ftell() to see how far into the file we are.
//The arguments for fseek() are the file pointer, the offset (how far to go from your base point),
//and base point (beginning, current position, or end)
fseek(file, 0, SEEK_END);
//The return value of ftell() is actually how many bytes into the file we are.
//However, since ASCII characters (such as ones stored in char strings) are one byte long,
//this is also a representation of how many characters there are in the file.
//Note that this includes special characters like \n to go to the next line
int fileLength = ftell(file);
//We're going to create a buffer in the memory to read our string into.
//If you're not familiar with dynamic memory allocation, feel free
//to contact me with more questions.
//A great article to explain the concept is found here:
//http://irc.essex.ac.uk/www.iota-six.co.uk/c/f7_dynamic_memory_allocation.asp
//I'm making the buffer 1 longer than the file length because the last byte of a string
//needs to be 0 to tell the computer the string has ended. This zero byte is called the
//"Null terminator", and is automatically added to strings when you declare them.
//malloc(), calloc(), and realloc() return a void* pointer, which you can use for whatever.
//We're casting it to a char* pointer so the compiler knows we're using it for a
//character string.
char* fileBuffer = (char*)calloc(fileLength + 1, 1);
//Use fseek() to go back to the beginning (you can't read a file backwards).
fseek(file, 0, SEEK_SET);
//Now we're going to read the file into the buffer. We call fread() to do this.
//The first argument is a pointer to your buffer, the second is the size of each element
//you're reading, the third is how many elements there are,
//and the final argument is a file pointer.
fread(fileBuffer, 1, fileLength, file);
//We're going to search for the start key. strstr() searches for a string inside another string
//and returns a pointer to the first instance it finds. We're going to add the length of the start
//key to the pointer it returns so that we know where what we're looking for starts, not the start key.
//strlen() returns the length of a string in bytes.
char* start = strstr(fileBuffer, startKey) + strlen(startKey);
//Calculate the length of our ouput string by finding the distance between the end of the start key
//and the start of the end key.
int outputLength = strstr(fileBuffer, endKey) - start;
//Note that we haven't accounted for what might happen if the end key is before the start key,
//if one of them doesn't exist, or other circumstances. If you want your code to be robust you might
//want to check for these things.
//create a new buffer to hold our output string and copy it in using memcpy().
//memcpy() takes the destination, the source, and how many bytes you want to copy.
//Keep in mind that your output is everything between the start and end key, including spaces.
char* output = (char*)calloc(outputLength + 1, 1);
memcpy(output, start, outputLength);
//Now that we've copied our output string to its own buffer, we can free the file buffer.
//Always, always free any memory you've allocated when you're done using it.
//Free memory you've allocated with malloc(), calloc(), or realloc() with free(),
//And any memory you've allocated with "new" with "delete".
//Generally, "new" and "delete" are better for use with C++ classes since they
//can be automatically constructed and deconstructed when you call them,
//but for simple buffers for strings and binary data, malloc() and free() work well.
//"new" and "delete" work for arrays too, so it's a preference thing.
//However, you can't resize a buffer using realloc() that you made with "new".
//Basically if you don't free your memory when you're done with it, the comptuer
//assumes that you're still using it and won't let any other program or process use it.
//This is called a "memory leak," and it can be very bad.
free(fileBuffer);
//I'm just printing it as an example, but you can now do whatever you want with your output string.
printf("My output string is:\n%s\n\n", output);
system("pause");
//Make sure to free your output buffer when you're done with it.
free(output);
//Always close the file when you're done with it so other programs can access it
//and so you don't leave the resources the program used for the file lying around.
fclose(file);
}