0

I've found that fprintf has no innate ability to adjust the colour of the output text and cprintf utilized through "conio.h" cannot print to a file (not that I could find anyway).

I want to be able to adjust the colour of the fprintf output to green within the "data.text" file.

static void cursor_position_callback(GLFWwindow* w, double x, double y)
{
    FILE *f = fopen("data.txt", "a");
    if (f==NULL)
    {
        printf("Error opening file!\n");
    }

    fprintf(f,"%0.3f: Cursor position: %f %f (%+f %+f)\n",
           glfwGetTime(),
           x, y, x - cursor_x, y - cursor_y);

    cursor_x = x;
    cursor_y = y;
    fclose(f);
}

The output will be used for mouse tracking data in an experiment to be run involving eye-hand tracking capabilities. Many thanks for your time.

zzz
  • 123
  • 1
  • 9
  • 4
    A text-file contains *text*, it has no other information. You might save [ASNI / VT100 escape codes](http://www.termsys.demon.co.uk/vtansi.htm) for colors in the file, but that will be saved as the raw escape sequences, it's up to the reader of the file to parse and handle the sequences. It will also make the file hard to read (by the user) if opened in a program that doesn't support the sequences, as the text will have embedded "weird" characters not relevant to the actual text. – Some programmer dude May 06 '16 at 22:24

2 Answers2

3

A file is just a sequence of bytes; its contents never has color. Color is something which only appears when displaying information (on a monitor or when printing on paper). However, the choice of what color to display can be affected by information in the file, if the program that reads the file knows how to react properly to the file contents. This essentially means that you need to define a (or use an existing) file format: rules for how the contents of a file may be structured and interpreted. For example (disregard the StackOverflow coloring; this is supposed to represent uncolored text in a text file):

Some of this text is <red>colored</red> in <blue>different</blue> ways.

Now, the program that is to display this must look for occurrences of <color>/</color> pairs and change the console color accordingly before showing the enclosed text. Note that if the text you want to display might itself happen to contain <red>, you need some sort of escaping mechanism, such as deciding that << will represent a literal <.

As you can tell, defining a file format is not easy, and you should probably stick with an established format that has existing parsers.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
0

In common practice the word "text" is often being (mis)used in its broader sense. In C programming practice the strict meaning of the term is also what's often being called "plain text" format (as opposed to "rich text" format and some other text formats). Let's examine this simple demonstration program:

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

int main(void)
{
FILE* testfile;
    fprintf(stdout, "%c\x1b[31m",27);    
    fprintf(stdout, "===================TITLE=================\n");
    fprintf(stdout, "%c\x1b[0m", 27); 
    fprintf(stdout, "This is a plain text coloration test in C\n");

    if((testfile = fopen("test.txt", "r" )) == NULL){
        testfile = fopen("test.txt", "w" );
        if(testfile != NULL ){
            fprintf(testfile, "%c\x1b[31m",27);    
            fprintf(testfile, "===================TITLE=================\n");
            fprintf(testfile, "%c\x1b[0m", 27); 
            fprintf(testfile, "This is a plain text coloration test in C\n");    
            fclose(testfile);    
        }
    }
return 0;
}

The terminal and shell may interpret the ANSI / VT 100 escape sequences as colors for as long as fprintf writes to stout or stderr, but if it writes to a regular file instead, this is what gets written to the file:

[31m===================TITLE=================
[0mThis is a plain text coloration test in C

user3078414
  • 1,942
  • 2
  • 16
  • 24