Skip to main content
added 150 characters in body; added 1 character in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

You have the right idea in write_to_file(), but print_file() is awkward.

There is no need to work a character at a time in print_file(). You can read a line at a time using fgets(buffer, sizeof(buffer), in_file).

Avoid flag variables. Here, you could use a for loop to track the line number instead.

char buffer[MAX_SIZE];
for (int i = 0; fgets(buffer, sizeof(buffer), in_file) != EOF; ++i) {
    if (i != 1) puts(buffer);
}
    

You have the right idea in write_to_file(), but print_file() is awkward.

There is no need to work a character at a time in print_file(). You can read a line at a time using fgets(buffer, sizeof(buffer), in_file).

Avoid flag variables. Here, you could use a for loop to track the line number instead.

You have the right idea in write_to_file(), but print_file() is awkward.

There is no need to work a character at a time in print_file(). You can read a line at a time using fgets(buffer, sizeof(buffer), in_file).

Avoid flag variables. Here, you could use a for loop to track the line number instead.

char buffer[MAX_SIZE];
for (int i = 0; fgets(buffer, sizeof(buffer), in_file) != EOF; ++i) {
    if (i != 1) puts(buffer);
}
    
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

You have the right idea in write_to_file(), but print_file() is awkward.

There is no need to work a character at a time in print_file(). You can read a line at a time using fgets(buffer, sizeof(buffer), in_file).

Avoid flag variables. Here, you could use a for loop to track the line number instead.