I am writing to a text file. Everything is working fine, except I can not figure out how to start a newline. I have tried

fprintf(txtFile, "%s", "\n");
fprintf(txtFile, "%s", '\n');
fprintf(txtFile, "%s\n", " ");

where txtFile is my FILE handle. How do I create a newline? Thanks.

fprintf(txtFile, "\n");
This should work.

Just to keep you informed here, use of %s is only when you need to store a variable.

Can i also point out that those are C functions. If your after the C++ solution it goes something like this

#include <ofstream>
#include <iostream>
using namespace std;

int main(void){
   ofstream out_file;
   out_file.open("example.txt");
   
   if(out_file.is_good()){
      out_file << "\nHello, World";
      out_file.close();
   }else{
        cout << "Error Opening File";
        cin.get();
   }

   return 0;
}

Chris

The 1st printf in the original post ( fprintf(txtFile, "%s", "\n"); ) should work too. The simplest way in C style is fputc('\n',txtFile) .

Apropos,

out_file << "\nHello, World";
     out_file.close();

makes ill-formed text file where the last line is not terminated by line separator...

Allow me to correct my error

out_file << "\nHello, World\n";
     out_file.close();
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.