user3440111
user3440111

Reputation: 5

Getting characters from a file in c

I wanted to get the characters of my text file. But I want it separately(spaces in the text separates the characters). This is my code for now.

main(){
    FILE *input_file = fopen("text.txt", "r");
    char char_check;
    char *string;
    int i = 0;

    while ((char_check = fgetc(input_file)) != EOF ){
        string = "";//resets the string here
        while(number_check != ' '){
            string[i] = number_check;
            i++;
        }
        printf("%s\n", string);
    }
}

The expected outcome if text.txt has "This is a text." on it: This\n is\n a\n text.

Upvotes: 0

Views: 147

Answers (2)

Utkan Gezer
Utkan Gezer

Reputation: 3069

With regards to your last edit as a response to my request, let's examine what modification you have made to the original text to obtain your desired result. The original text is:

    This is a text.

And you've turned that into:

    This\n is\n a\n text.

I hope that this is as clear to your eyes as it is to mine; but there, you have inserted a new-line '\n' right before each occurrence of space ' '. So, you could do exactly that by;

  • getting a character right before cycle in a while loop, a loop that would end when the file ends
  • if the acquired character is a ' ', then printing a '\n'
  • after that printing the acquired character, no matter what it was

Like this:

...
    while ( ( char_check = fgetc( input_file ) ) != EOF ){
        if ( char_check == ' ' ) {
            printf( "\n" );
        }
        printf( "%c", char_check );
    }
...

There were several problems with your code that would cause it to not work at all. You are using a variable named number_check that you haven't initialized. You are trying to assign values to string[i] for any i, you may not do that.

To do that, you first need a space. To have the space, you can;

// initialize string as an array
// which will automatically allocate memory for you to use
char string[123];

// manually allocate a memory to use
char * string;
string = malloc( 123 );

// or better
string = malloc( 123 * sizeof( char ) );

// or even better
string = malloc( 123 * sizeof( * string ) );

// but if you use malloc, remmeber to use free, to free the memory after using it
// !! only if you use malloc/calloc/realloc !!
free( string );

Forget about the assignment string = "";, it won't empty out an array, nor grant you an empty array, as would like to have.

Just reset i back to 0 before starting each loop and overwrite the previous characters.

Appending a sequence of characters (string) with a '\0', which is simply zero, is a must. If you don't do that, printf( "%s", string ); won't know when your string will end. You may think of that zero, the terminating zero, as a full-stop to your whole string.

Anyway, I think this much information should suffice. Let's re-write your original code to have it work:

#include <stdio.h>
#include <stdlib.h>     // <-- included stdlib.h for malloc

int main( ){
    FILE *input_file = fopen( "text.txt", "r" );
    char char_check;
    char *string;
    int i = 0;

    string = malloc( 321 * sizeof( *string ) );     // <-- allocated memory for string

    while ( ( char_check = fgetc( input_file ) ) != EOF ){

        i = 0;      // <-- resetting i back to zero

        while ( char_check != ' ' && char_check != EOF ){   // <-- added an EOF check
            string[i] = char_check;
            i++;
            char_check = fgetc( input_file );   // <-- added this, to obtain next
        }
        string[i] = '\0';               // <-- appended with zero, important

        printf( "%s\n ", string );      // <-- added a space after the \n
    }

    free( string );     // freed the allocated memory after use

    return 0;
}

Upvotes: 0

suspectus
suspectus

Reputation: 17258

If you are trying to print out the file character by character with spacing, you could simply print out each character followed by a space.

main(){
    FILE *input_file = fopen("text.txt", "r");
    int number_check;

    while ((number_check = fgetc(input_file)) != EOF ){
        printf("%c ", number_check);
    }
}

Upvotes: 3

Related Questions