// use these code boxes please its actually fucking readable
// ignore everything else, this is just rewriting it so IT'S READABLE
/* Includes */
#include <stdio.h>
/* Main */
int main( int argc, char *argv[] )
{
// Var decs
FILE *fp = NULL;
int i = 0;
// Open file for read
fp = fopen("sample.txt", "r");
// If fp is 0? really should be comparing this to null, man, not all compilers like this
// And it's not good practice to have a return in the middle of a function
if ( fp == null )
{
// this gives an error to windows. how dare you
return 1;
}
else
{
do
{
i = fgetc( fp );
if ( i!=EOF )
printf( "%c", i );
}
while ( i != EOF );
fclose( fp );
return 0;
}
}
Is this C or C++?
I mean, you could use fscanf. Use spaces a limiters.
Once you got that, throw the string into your array of strings.
I have absolutely no idea if these work, but they will give you an idea on how to approach the problem.
/* add_word bla bla
* returns 1 on added, 0 on dupes
* note: array 'words' must have a null entry as the last one. like a string.
* note: words must have been created with malloc, or there will be memory error
* note: if words is null, disregard previous note
*/
int add_word ( char *** words, char * word )
{
/* Local Vars */
int i = 0;
int words_size = 0;
int can_add = 1;
char ** new_words = NULL;
// Find size of words
while (( (*words) != null )&&( (*words)[words_size] != NULL )) // short circuit!
{
words_size += 1;
}
// check if word is unique
for ( i = 0; ( i < words_size )&&( can_add ); i += 1 )
{
if ( strcmp( (*words)[i], word ) == 0 )
{
can_add = 0;
}
}
// Add the word if we can_add the word.
if ( can_add )
{
new_words = malloc( sizeof( char* ) * ( words_size + 2 ) );
for ( i = 0; i < words_size; i += 1 )
{
new_words[i] = (*words)[i];
}
new_words[ words_size ] = word;
new_words[ words_size+1 ] = NULL;
if ( words == NULL )
{
free( words );
}
words = &new_words;
return 1;
}
else
{
return 0;
}
}
C++
/* add_word bla bla
* returns 1 on added, 0 on dupes
* note: array 'words' must have a null entry as the last one. like a string.
*/
int add_word ( std::string ** words, std::string word )
{
int words_size = 0;
bool can_add = true;
// Find size of words
while (( (*words) != null )&&( (*words)[words_size] != NULL )) // short circuit!
{
words_size += 1;
}
// check if word is unique
for ( i = 0; ( i < words_size )&&( can_add ); i += 1 )
{
if ( (*words)[i].compare( word ) == 0 )
{
can_add = false;
}
}
// Add the word
if ( can_add )
{
std::string * new_words = NULL;
new_words = new (std::string*) [words_size + 2];
for ( i = 0; i < words_size; i += 1 )
{
new_words[i] = (*words)[i];
}
new_words[ words_size ] = word;
new_words[ words_size+1 ] = NULL;
if ( words == NULL )
{
delete words;
}
words = &new_words;
return 1;
}
else
{
return 0;
}
}
Now, I'll say again, I'm not sure these work - actually I don't think they do looking at them again, I'm writing this while doing my physics homework, phuck mastering physics, but it should give you an idea.