Greetings,

And an advanced "thank you very much" if you can help me :)

I just played with the great tutorial from Dave Sinkula
http://www.daniweb.com/code/snippet151.html

but I'm having trouble altering it.

I want it to read the file, and each one of the lines, and output something like this:

1cat2cat3
1dog2dog3
1bird2bird3

but instead of that I'm getting

1cat
2cat
3
1dog
2dog
3
1bird2bird3

Only the last line is being done correctly. I must change something, but I guess I'm too tired and I didn't figured it out.

Can you help me? File.txt has this:

cat
dog
bird

The code I'm using is:

#include <stdio.h>

int main ( void )
{

  static const char filename[] = "file.txt";
   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
      char line [ 128 ];

      while ( fgets ( line, sizeof line, file ) != NULL )
      {
printf("1");
fputs ( line, stdout );
printf("2");
fputs ( line, stdout );
printf("3\n");
      
}
      fclose ( file );
   }
   else
   {
      perror ( filename );
   }
   return 0;
}

I've also tried with fputs but the results were the same...

Thank you for your attention :cheesy:

Dani AI

Generated

Short version: the extra line breaks come from the line buffer still containing the end‑of‑line characters read by fgets. fixed it for by removing the stored newline; that is the right idea. A few extra notes and practical alternatives that are useful years later.

A compact, robust way to trim both Unix and Windows line endings is to remove any trailing \n or \r right after reading the line. The strcspn trick is concise and safe:

#include <string.h>

static void chomp(char *s) {
    s[strcspn(s, "\r\n")] = '\0';
}

If the platform supports getline (POSIX/glibc), it simplifies buffer management and avoids fixed-size truncation:

char *line = NULL;
size_t len = 0;
ssize_t nread;
while ((nread = getline(&line, &len, file)) != -1) {
    chomp(line);
    printf("1%s2%s3\n", line, line);
}
free(line);

Extra practical tips:

  • If you must use fgets, watch for truncated lines: if the buffer fills and the last character is not \n, consume and discard the remainder of that line before continuing.
  • fputs writes the string exactly as stored; it does not append or remove newlines. Trimming the buffer first is the simplest fix.
  • Prefer size_t/ssize_t for sizes and check return values from I/O calls. Use perror or errno to report I/O errors.
  • Remember that the final file line may legally lack a newline; robust code should handle that case without assuming every read ends with \n.

Acknowledgement: provided the in-place trim solution that solved the original symptom reported by , and confirmed the fix worked.

Recommended Answers

All 4 Replies

>I've also tried with fputs but the results were the same...
The problem isn't with your output, it's with your input. fgets stores the newline character. Because only the last line isn't terminated with a newline, it's the only line that prints correctly. Try this:

#include <stdio.h>
#include <string.h>

int main ( void )
{
  static const char filename[] = "file.txt";
  FILE *file = fopen ( filename, "r" );

  if ( file != NULL )
  {
    char line [ 128 ];

    while ( fgets ( line, sizeof line, file ) != NULL )
    {
      char *newline = strchr ( line, '\n' );

      if ( newline != NULL )
        *newline = '\0';

      printf("1");
      fputs ( line, stdout );
      printf("2");
      fputs ( line, stdout );
      printf("3\n");

    }

    fclose ( file );
  }
  else
  {
    perror ( filename );
  }

  return 0;
}

Thank you!

I will try it :)

It really worked, Narue :)

Thank you! Thank you! Thank you!

It really worked, Narue :)

Yep, Narue's good! Welcome Back!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.