1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
int main()
{
// String Storage
char mystring[100];
// Format into words
int character_scanner = 0;
int begin_word = 0;
// Compact
int file_discription;
int file_length;
char *buffer;
char word_pos[10];
char *words;
FILE *outfile; // DHH declare a pointer to a file control block.
// Create and open a file
/*
* DHH this is the line that was causing the most problems. The "r+" requires that
* input file already exist. Since it doesn't, the fopen failed and returned a null
* pointer as the FILE pointer. This caused later file operations to fail.
* Use "w+" if you want to create or overwrite an existing file.
*/
outfile = fopen("Hello_World.txt", "w+");
// ********************************
// Print a character string in the file, and set the cursor to the beggining
// ( DHH long string, multiple adjacent strings are concatenated by the compiler
// into one long string. This works as long as there are no % characters in it
// because it occupies the position of the format string in the fprintf function call)
// A safer implementation would be:
//
// fprintf(outfile, "%s", "Hello World. This would happen to be the string "
// "being tested for whitespace!\n");
//
// -or-
//
// fprintf(outfile, "%s\n", "Hello World. This would happen to be the string "
// "being tested for whitespace!\n");
fprintf(outfile, "Hello World. This would happen to be the \nstring being tested for whitespace!\n");
fseek(outfile, 0, SEEK_SET);
// ***************************************************************************
// Aquire the first string in the file
#if 0
/*
DHH This is the better way to comment out code (using #if 0).
First off it allows you to put old code back in place if you need
to try something else. Secondly, it is not subject to problems with
nested comments.
*/
fgets(mystring, 100, outfile);
#else /* DHH optional. */
#endif
// **************************
// Put declarations at top of code
// initialize variables here to check the character for a white space.
// **************************************
// Check each character for white space, start at the pointer(character_scanner
// end at NULL
/*
* DHH This version of fgets makes it unnecessary to search for every use of mystring
* to make sure you don't declare it with multiple lengths. I practice, the
* only constants I allow in my code are 1 and 0. Anything else is declared
* as a #define or a variable. It prevents a lot of unnecessary debuging later.
*/
while (fgets(mystring, sizeof mystring, outfile))
{
/*
* OK, we got a line of input for parsing.
* initialize the character scanner to start of line.
*/
character_scanner = 0;
/*
* Now we loop processing the line.
*/
while (mystring[character_scanner] != NULL) // DHH
{
begin_word = character_scanner;
if (!isspace(mystring[character_scanner]))
{
while (!isspace(mystring[++character_scanner]));
mystring[character_scanner++] = '\0';
}
else
{
begin_word = ++character_scanner;
}
}
}
// ***************************************
// Compact the first word in the string to the first character in the array
// ********************************
// set the buffer to the address of the string
file_discription = _open("Hello_World.txt", _O_RDONLY);
file_length = _filelength(file_discription);
buffer = (char*) malloc (file_length);
// Register spaces in the buffer to array word_pos
words = strchr(buffer, '\0');
while(words != NULL)
{
strcpy(word_pos, words);
words = strchr(buffer + 1, '\0');
}
// print the data at the specified address to the screen
printf(word_pos);
fclose(outfile);
}
|