Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Reading a file and print the content in C

I'm learning how to write and read files in C, and I wrote a text using this code

 FILE *f = fopen("testingText.txt", "w");
 char *text = "This is text1...";
 fwrite(text, sizeof(char), strlen(text), f );
 fclose(f);

and when I read the content of this file and print it using this code

 FILE *f = fopen("testingText.txt", "r");
 fseek(f, 0, SEEK_END);
 unsigned int size = ftell(f);
 fseek(f , 0, SEEK_SET);
 char *content = (char *)malloc(size);

 fread(content, sizeof(char), size, f);
 printf("File content is...\n%s", content);


 free(content);
 fclose(f);

it gives the result with strange things like these

File content is... This is text1...Path=C:*┬#æ╩eò*

and when I run the code again it gives different strange things.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

There is no null terminator in the file so you'll need to add that manually before printing what you've read from the file.

Example:

char *content = malloc(size + 1);               // +1 for the null terminator
size_t chars_read = fread(content, 1, size, f); // store the returned value
content[chars_read] = '\0';                     // add null terminator
printf("File content is...\n%s\n", content);    // now ok to print
over 4 years ago · Santiago Trujillo Report

0

The following line is wrong:

printf("File content is...\n%s", content);

Using printf with the %s conversion format specifier requires a null-terminated string. However, your string is not null-terminated.

In order to print a sequence of characters that is not null-terminated, you can write the following instead:

printf( "File content is...\n%.*s", (int)size, content );

Or you can add a terminating null character manually, with the following line:

content[size] = '\0';

However, this will write to the memory buffer content out of bounds, because you did not allocate any space for the null terminating character. Therefore, you should allocate one additional byte in the malloc function call.

Another problem is that using ftell is not a reliable way to determine the length of the file. The ISO C standard does not guarantee that this will work.

For example, on Microsoft Windows, this will give you the length of the file in binary mode (even when the file is opened in text mode). However, the length of the file in text mode is different, because \r\n line endings get translated to \n on Microsoft Windows.

Therefore, if you want to read the content of a text file of unknown length, it would probably be better to read one line at a time in a loop, using the function fgets:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    FILE *fp;
    char line[100];

    //attempt to open file
    fp = fopen( "testingText.txt", "r" );

    //verify that file is open
    if ( fp == NULL )
    {
        fprintf( stderr, "error opening file!\n" );
        exit( EXIT_FAILURE );
    }

    printf( "File content is...\n" );

    //print one line per loop iteration
    while ( fgets( line, sizeof line, fp ) != NULL )
    {
        //the following code will work even if "fgets" only
        //reads a partial line, due to the input buffer not
        //being large enough

        //print the line to standard output
        fputs( line, stdout );
    }

    //cleanup
    fclose( fp );
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!