Below is the c version. However, I must say that I prefer the c++ version. :-) https://stackoverflow.com/a/3910610/278976
main.c
#include <stdio.h>
#include <string.h>
#define BUFFER_SIZE 1024
int main( int argc, char** argv ){
const char *delimiter_characters = " ";
const char *filename = "file.txt";
FILE *input_file = fopen( filename, "r" );
char buffer[ BUFFER_SIZE ];
char *last_token;
if( input_file == NULL ){
fprintf( stderr, "Unable to open file %s\n", filename );
}else{
// Read each line into the buffer
while( fgets(buffer, BUFFER_SIZE, input_file) != NULL ){
// Write the line to stdout
//fputs( buffer, stdout );
// Gets each token as a string and prints it
last_token = strtok( buffer, delimiter_characters );
while( last_token != NULL ){
printf( "%s\n", last_token );
last_token = strtok( NULL, delimiter_characters );
}
}
if( ferror(input_file) ){
perror( "The following error occurred" );
}
fclose( input_file );
}
return 0;
}
file.txt
Hello there, world!
How you doing?
I'm doing just fine, thanks!
linux shell
root@ubuntu:/home/user# gcc main.c -o example
root@ubuntu:/home/user# ./example
Hello
there,
world!
How
you
doing?
I'm
doing
just
fine,
thanks!